使用Java查找并替换多个文本文件中的单词?

时间:2011-10-15 17:00:17

标签: java

如何使用Java找到并替换多个文本文件中的单词?

以下是我为单个String ...

执行此操作的方法
public class ReplaceAll {

    public static void main(String[] args) {
        String str = "We want replace replace word from this string";  
        str = str.replaceAll("replace", "Done");
        System.out.println(str);
    }
}

3 个答案:

答案 0 :(得分:7)

使用Commons IO中的FileUtils

String[] files = { "file1.txt", "file2.txt", "file3.txt" };
for (String file : files) {
    File f = new File(file);
    String content = FileUtils.readFileToString(new File("filename.txt"));
    FileUtils.writeStringToFile(f, content.replaceAll("hello", "world"));
}

答案 1 :(得分:2)

您可以使用BufferedReader包装的FileReader读取文件,逐行拉出,对您在问题中显示的字符串执行相同的替换,然后将其写回新文件。

答案 2 :(得分:1)

这是工作代码:希望它有所帮助!

import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;


public class TestIO {

 static StringBuilder sbword = new StringBuilder();
 static String dirname = null;
 static File[] filenames = null;
 static Scanner sc = new Scanner(System.in);

public static void main(String args[]) throws FileNotFoundException, IOException{
    boolean fileread = ReadFiles();

    sbword = null;
    System.exit(0);



}
private static boolean ReadFiles() throws FileNotFoundException, IOException{

    System.out.println("Enter the location of folder:");


    File file = new File(sc.nextLine());
    filenames = file.listFiles();
    String line = null;

    for(File file1 : filenames ){
    System.out.println("File name" + file1.toString());
    sbword.setLength(0); 
    BufferedReader br = new BufferedReader(new FileReader(file1));

    line = br.readLine();

    while(line != null){
        System.out.println(line);
        sbword.append(line).append("\r\n");
        line = br.readLine();
        }

    ReplaceLines();
    WriteToFile(file1.toString());

    }

    return true;    
}

private static void ReplaceLines(){
    System.out.println("sbword contains :" + sbword.toString());
    System.out.println("Enter the word to replace from each of the files:");
    String from = sc.nextLine();
    System.out.println("Enter the new word");
    String To = sc.nextLine();

    //StringBuilder sbword = new StringBuilder(stbuff);
    ReplaceAll(sbword,from,To);



}
private static void ReplaceAll(StringBuilder builder, String from, String to){
    int index = builder.indexOf(from);
    while(index != -1){
        builder.replace(index, index + from.length(), to);
        index += to.length();
        index = builder.indexOf(from,index);
    }
}
private static void WriteToFile(String filename) throws IOException{
   try{
    File file1 = new File(filename);
    BufferedWriter bufwriter = new BufferedWriter(new FileWriter(file1));
    bufwriter.write(sbword.toString());
    bufwriter.close();

    }catch(Exception e){
         System.out.println("Error occured while attempting to write to file: " +     e.getMessage());
    }

}

}

import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class TestIO { static StringBuilder sbword = new StringBuilder(); static String dirname = null; static File[] filenames = null; static Scanner sc = new Scanner(System.in); public static void main(String args[]) throws FileNotFoundException, IOException{ boolean fileread = ReadFiles(); sbword = null; System.exit(0); } private static boolean ReadFiles() throws FileNotFoundException, IOException{ System.out.println("Enter the location of folder:"); File file = new File(sc.nextLine()); filenames = file.listFiles(); String line = null; for(File file1 : filenames ){ System.out.println("File name" + file1.toString()); sbword.setLength(0); BufferedReader br = new BufferedReader(new FileReader(file1)); line = br.readLine(); while(line != null){ System.out.println(line); sbword.append(line).append("\r\n"); line = br.readLine(); } ReplaceLines(); WriteToFile(file1.toString()); } return true; } private static void ReplaceLines(){ System.out.println("sbword contains :" + sbword.toString()); System.out.println("Enter the word to replace from each of the files:"); String from = sc.nextLine(); System.out.println("Enter the new word"); String To = sc.nextLine(); //StringBuilder sbword = new StringBuilder(stbuff); ReplaceAll(sbword,from,To); } private static void ReplaceAll(StringBuilder builder, String from, String to){ int index = builder.indexOf(from); while(index != -1){ builder.replace(index, index + from.length(), to); index += to.length(); index = builder.indexOf(from,index); } } private static void WriteToFile(String filename) throws IOException{ try{ File file1 = new File(filename); BufferedWriter bufwriter = new BufferedWriter(new FileWriter(file1)); bufwriter.write(sbword.toString()); bufwriter.close(); }catch(Exception e){ System.out.println("Error occured while attempting to write to file: " + e.getMessage()); } }