zxd7900663 发表于 2013-2-7 16:16:59

目录(包含子目录)下文件内容替换

本例子的功能是查找目录(包括所有子目录)下所有指定后缀的文件(如果不指定后缀则为全部文件)并替换内容
看代码:
   package file;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;/** * 查找目录(包括所有子目录)下所有html文件并替换内容 * @author zxd * */public class FileViewer {public static void main(String[] args) throws IOException {   List arrayList = FileViewer.getListFiles("D:/java/workspace/vss/vss/srm/crealib/api","html",true);//   List arrayList = FileViewer.getListFiles("D:/yfb_doc","html",true);   if(arrayList.isEmpty())   {    System.out.println("没有符合要求的文件");   }   else   {    StringBuffer message = new StringBuffer();    message.append("符合要求的文件数:");    message.append(arrayList.size());    message.append("\r\n");    //message += "符合要求的文件数:" + arrayList.size() + "\r\n";    System.out.println(message);       for (Iterator i = arrayList.iterator(); i.hasNext();)    {   String temp = (String) i.next();          String newfilecontent = readfile(temp).replaceAll("<B>FRAMES</B>", "");   java.io.RandomAccessFile file =   newjava.io.RandomAccessFile(temp, "rw" );            file.setLength( 0 );            // 写入新的文件内容      appendMethod(temp,newfilecontent);          System.out.println(temp);   //message += temp + "\r\n";   message.append(temp + "\r\n");    }       //appendMethod("d:/ajax/menu.txt",message);   }}public static List<String> fileList = new ArrayList<String>();/*** * @param path 文件路径* @param suffix 后缀名* @param isdepth 是否遍历子目录* @return*/public static List getListFiles(String path, String suffix, boolean isdepth) {   File file = new File(path);   return FileViewer.listFile(file ,suffix, isdepth);}public static List listFile(File f, String suffix, boolean isdepth) {   //是目录,同时需要遍历子目录   if (f.isDirectory() && isdepth == true)   {    File[] t = f.listFiles();    for (int i = 0; i < t.length; i++)    {   listFile(t, suffix, isdepth);    }   }   else    {    String filePath = f.getAbsolutePath();       if(suffix !=null)    {   int begIndex = filePath.lastIndexOf(".");//最后一个.(即后缀名前面的.)的索引   String tempsuffix = "";         if(begIndex != -1)//防止是文件但却没有后缀名结束的文件   {      tempsuffix = filePath.substring(begIndex + 1, filePath.length());   }         if(tempsuffix.equals(suffix))   {      fileList.add(filePath);   }    }    else    {   //后缀名为null则为所有文件   fileList.add(filePath);    }      }   return fileList;}//读取文件内容public static String readfile(String path){   String readStr = "";try{   String read = "";String filepath=path;// 得到文本文件的路径    File file=   newFile(filepath);   FileReader fileread=   newFileReader(file);   BufferedReader bufread=   newBufferedReader(fileread);   while((read= bufread.readLine())!=null){   read=read + " \r\n " ;               readStr=readStr+read;   }    }catch(Exception e){   System.out.println(e.getMessage());   }    returnreadStr;// 返回从文本文件中读取内容    } /**    * 方法追加文件:使用FileWriter    * @param fileName    * @param content    */public static void appendMethod(String fileName, String content){    try   {   //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件   FileWriter writer = new FileWriter(fileName, true);   writer.write(content + "\r\n");   writer.close();    }   catch (IOException e)   {   e.printStackTrace();    }}}
页: [1]
查看完整版本: 目录(包含子目录)下文件内容替换