-1

How do you replace a line in a text file?

For example you have 1.@@@ and want to replace it with 1.###

I have program this prgram at the moment. You search through a list and if you find a string, that you want. You write the string to another file. My porblem is that I don't know how to replace a line in an existing text file.

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){}

}        

and this is the list.

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;

user2699992
  • 7
  • 1
  • 3
  • possible duplicate of [Java read file line by line and replace nth column](http://stackoverflow.com/questions/18478820/java-read-file-line-by-line-and-replace-nth-column) – Jim Garrison Aug 29 '13 at 17:19
  • I answered a question similar to this a couple of days ago. I know it isn't *exactly* what are you looking for, but it will give you a general idea on how to rewrite a `File`. All you need to do is make some few minor modifications. Refer to [this](http://stackoverflow.com/a/18479057/1255746). Feel free to ask any questions that you may have. – Josh M Aug 29 '13 at 17:17

1 Answers1

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();
    }
}
thriqon
  • 2,458
  • 17
  • 23