Results 1 to 3 of 3
  1. #1
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default write to txt file

    Hello guys. I have create two txt files and I wan to parse things there.

    Java Code:
    FileWriter outFile1 = new FileWriter("newFaces.txt", true);
            FileWriter outFile2 = new FileWriter("newVertices.txt", true);
            PrintWriter out1 = new PrintWriter(outFile1);
            PrintWriter out2 = new PrintWriter(outFile2);
    and I want to parse some variables inside a for loop but i want iteration to add new lines in the txt(to append lines).
    Java Code:
    for (int i=0;i<4; i++){ //for example
                           out2.append(newvertex1+" "+newvertex2+" "+ newvertex3);
                           out2.println();
    
                           out2.close();
                           
                           out1.append(aString+" "+aString1+" "+ aString3); 
                           out1.println();
                           out1.append(aString+" "+aString1+" "+ aString4);
                           out1.println();
                           out1.append(aString+" "+aString2+" "+ aString3);
                           out1.println();
                           out1.append(aString+" "+aString2+" "+ aString4);
                           out1.println();
                           out1.close();
    
    }
    This work like a charm only for the first iteration. In the end of the loop i ve onlyl the data of the first iteration. Have you any idea upon this matter? What i m doing wrong?

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Why exactly are you closing your PrintWriter's in your loop? Close your PrintWriter's when the loop is done; that's the reason why only your first iteration is being written.

  3. #3
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2010, 06:31 AM
  2. Replies: 8
    Last Post: 05-12-2010, 08:19 PM
  3. how to write onto a file
    By mirage_87 in forum New To Java
    Replies: 6
    Last Post: 09-08-2009, 04:54 PM
  4. Replies: 5
    Last Post: 02-05-2009, 11:28 AM
  5. [SOLVED] File Write in File
    By Eku in forum New To Java
    Replies: 4
    Last Post: 10-27-2008, 05:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •