-1

I want to print my console lines to txt file.I want this operation in else statement.But it writes to file only last record.How can i fixed it? When i try buffered reader it cant solve my problem.Thanks everybody.Have a good day.Which solution do you advice to me?

for(int iy=0;iy<arr.length;iy++){

    if( arr[iy].contains("•") || arr[iy].contains("Contact") || arr[iy].contains("@") || arr[iy].contains("Activities and Societies") || arr[iy].contains("Page")){

    }               
    else if(arr[iy].contains("Summary")){
        //System.out.println(arr[iy]+"enes");
        while(exit==false){
            iy++;

            if(arr[iy].contains("Experience")){
                exit=true;
            }
        }           
    }               
    else if(arr[iy].contains("Skills") || arr[iy].contains("Languages")){

        while(exit==false){
            iy++;

            if(arr[iy].contains("Education")){
                exit=true;
            }
        }
    }                               
    else  
    {                   
        System.out.println(arr[iy]); 

        try(PrintWriter bw = new PrintWriter(new FileWriter(FILENAME2))){

            bw.write(arr[iy]);
            //out.newLine();
            bw.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }                       
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
TheFlash
  • 25
  • 5
  • 2
    Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the "edit" link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Aug 02 '17 at 08:10
  • 2
    Beyond that: you want us to spend *our* time to help you solve your problem. You should make that as easy as possible. So please spend the 1 minute it takes to properly indent/format all of your source code. You know, that *preview* window exists for a reason - same for the many explanations around the edit window. – GhostCat Aug 02 '17 at 08:11
  • 2
    Finally: read about *clean* code. This code is 10 times harder to read than it ought to be (not only because of the lazy formatting, but also because of the overly complicated structure). It is no surprise that you need other people to identify the problems in this mess. – GhostCat Aug 02 '17 at 08:14

2 Answers2

0

Looks like you're re-creating the PrintWriter at every step of the loop, which overwrites the file. Create it before the loop and close it afterwards.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

Your code is very convoluted, but the reason for just the last line being written is clear - you open the file for each line you are about to output to it, and overwrite the previous contents of the file.

  • You shouldn't open and close the file for each line. Open it once before the loop, and close it after the loop.

  • If it did make sense to open and close the file multiple times, you should have opened the FileWriter in append mode (by using new FileWriter(FILENAME2,true)).

Eran
  • 387,369
  • 54
  • 702
  • 768