使用java替换文本文件中的一行

时间:2013-08-29 17:09:56

标签: java file text replace

如何替换文本文件中的一行?

例如你有1. @@@ 并希望用1替换它。###

我现在已编程此程序。 您搜索列表,如果找到了所需的字符串。您将字符串写入另一个文件。我的问题是我不知道如何替换现有文本文件中的一行。

private static BufferedReader br;



public static void main(String[] args) throws Exception{


    try{
    FileInputStream fstream = new FileInputStream("C:\\Users\\Timmic\\workspace\\Foutverbeterende codes\\genereren append testbinair apart.txt");
    br = new BufferedReader(new InputStreamReader(fstream));
    String strLine;


    //Read File Line By Line
    while ((strLine = br.readLine()) != null) {
        String tokens[] = strLine.split(";");
        int x = Integer.parseInt(tokens[2]);



        if(x<2){
            tokens[3]="###";

            String a1 = Arrays.toString(tokens);
            String a2 = a1.replaceAll("  ", "");
            String a3 = a2.replaceAll(" ", "");
            String a6 = a3.replaceAll(",", ";");
            String a7 = a6.replaceAll("[<>\\[\\],-]", "");
            String a8 = a7 + ";";


            System.out.println(a8);





            FileWriter fwriter = new FileWriter("d is 2.txt", true);
            PrintWriter outputFile = new PrintWriter(fwriter);

            outputFile.println(a8);
            outputFile.close();

        } 



    }


    }


    catch(Exception e){}

}        

这就是清单。

0; 000; 0; * ; 0; 0; 0;

1; 001; 1; * ; 0; 0; 1;

2; 010; 1; * ; 0; 1; 0;

3; 011; 2; * ; 0; 1; 1;

4; 100; 1; * ; 1; 0; 0;

5; 101; 2; * ; 1; 0; 1;

6; 110; 2; * ; 1; 1; 0;

7; 111; 3; * ; 1; 1; 1;

1 个答案:

答案 0 :(得分:0)

// it's okay to throw Exception in main, but ONLY during testing. NEVER EVER
// in production code!
public static void main(String[] args) throws Exception{
    FileWriter fwriter = null;
    FileInputStream fstream = null;
    try {
        fstream = new FileInputStream("C:\\Users\\Timmic\\workspace\Foutverbeterende codes\\genereren append testbinair apart.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;

        // this has to be moved OUT of the loop.
        fwriter = new FileWriter("d is 2.txt", true);
        PrintWriter outputFile = new PrintWriter(fwriter);


        //Read File Line By Line
        while ((strLine = br.readLine()) != null) {
            String tokens[] = strLine.split(";");
            int x = Integer.parseInt(tokens[2]);

            if(x<2){
                tokens[3]="###";

                String replaced = Arrays.toString(tokens)
                                .replaceAll("  ", "");
                                .replaceAll(" ", "");
                                .replaceAll(",", ";");
                                .replaceAll("[<>\\[\\],-]", "");
                replaced += ";";

                System.out.println(replaced);

                outputFile.println(replaced);
            } 
        }

    // finally makes sure, that this block is executed, even if something
    // goes wrong.
    } finally {
        if (fstream != null)
            fstream.close();
        if (fwriter != null)
            fwriter.close();
    }
}