2011-10-15 73 views
1

如何使用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); 
    } 
} 

回答

7

運用Commons IOFileUtils

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")); 
} 
+0

好的,我必須這樣的代碼: – Pacific

+0

這段代碼無法保證嗎? import java.io.File; 公共類FindNReplace { 公共靜態無效主要(字符串ARGS []){ 字符串 []文件= { 「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」)); }} } – Pacific

+0

對不起,我不知道該怎麼回答 – Pacific

2

您可以使用的FileReader由一個BufferedReader包裹,通過線拉出符合該文件中讀取後,對相同的替換您在問題中顯示的字符串,並將其寫回新文件。

+0

如果您想使用'「\ n」'替換'「\ r \ n」',那麼不是。 – aioobe

+0

@aioobe我不明白爲什麼這會特別成問題。顯然,如果你需要替換比一行更長的東西,它會更復雜一些,但仍然比一次將整個文件存儲在內存中要好。 – Voo

+0

我真的不明白你 – Pacific

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()); 
    } 

} 

}

相關問題