-1

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

例如,您有 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;

4

1 回答 1

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();
    }
}
于 2013-08-29T17:21:05.823 回答