Results 1 to 9 of 9
  1. #1
    hakan123 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default Writting output to file!

    Hello All!
    I am a java beginner. I have an output to the console, which is of two lines i.e.line1 and then line2 as a new line.
    i want to write my output to a text file.
    im using.
    PrintWriter out = new PrintWriter(new FileWriter("output.txt"));

    out.append(result);
    out.close();

    whereas my result is charachters.
    now what happens is only the last line of output is written to the text file, and the cursor position is at the very start of the file.
    thought the first line is also written, but as soon as i get the second line on the console, the second line is overwritted on the first line in the text file.

    i want to write my output as it appears on the console.
    please help

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You are overwriting the file each time you access it. You're not appending. Have a look at RandomAccessFile, that might help, but I'm not sure.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    hakan123 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default

    Thanks friend!
    yes i worry that i am overwritting. and i want to avoide over-writting.how can i avoide it?

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Have you looked at RandomAccessFile? You could also read the file, append the new message and write the whole lot back to disk.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    hakan123 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default

    i am having a look at Randomaccessfile. i hope that it will help. if any prob, i will get back to here..
    thanks for your help:)

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    [QUOTE=hakan123;91898]

    Use this instead:

    Java Code:
    PrintWriter out = new PrintWriter(new FileWriter("output.txt", true));
    The second boolean parameter tells the writer to append to the file if it exists. Read the API documentation for the FileWriter class.

    kind regards,

    Jos

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Dear lord, maybe I should read the API from time to time.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    hakan123 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default

    Thanks Joash for your kind reply!
    I tried it with both randomfileaccess....and append bolean true....
    its working with both....but i want to use append bolean true...i also want to have a new txt file for every output.in another way, i want to have a check..if the text file already exists, it should be deleted..and a new txt file should be defined for the current output.....im trying to use...if.exit,and file.delets..but it gives me errorr..im trying:
    PrintWriter out = new PrintWriter(new FileWriter("foo.txt", true));
    if (out.exists())
    {
    out.delete();
    PrintWriter out = new PrintWriter(new FileWriter("foo.txt", true));
    out.println(output);
    }
    else {
    out.println(output);
    }

    out.close();


    the errorr is:
    The method exists() is undefined for the type PrintWriter
    The method delete() is undefined for the type PrintWriter
    Duplicate local variable out..

    please help

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by hakan123 View Post
    Thanks Joash for your kind reply!
    I tried it with both randomfileaccess....and append bolean true....
    its working with both....but i want to use append bolean true...i also want to have a new txt file for every output.in another way, i want to have a check..if the text file already exists, it should be deleted..and a new txt file should be defined for the current output.....im trying to use...if.exit,and file.delets..but it gives me errorr..im trying:
    PrintWriter out = new PrintWriter(new FileWriter("foo.txt", true));
    if (out.exists())
    {
    out.delete();
    PrintWriter out = new PrintWriter(new FileWriter("foo.txt", true));
    out.println(output);
    }
    else {
    out.println(output);
    }

    out.close();


    the errorr is:
    The method exists() is undefined for the type PrintWriter
    The method delete() is undefined for the type PrintWriter
    Duplicate local variable out..

    please help
    As the compiler says: there are not exists() nor delete() methods defined in the PrintWriter class; who gave you the idea they did? Use the File class instead and please read the API documentation before you start coding so you don't have to guess.

    b.t.w. you can't delete a file when it is open for either reading or writing. If the file exists you want to delete it; what's the deal with opening in append mode anyway? Please rethink your business logic before you start to code.

    kind regards,

    Jos

Similar Threads

  1. problem with output of jar file
    By nishant.4545 in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2009, 04:00 PM
  2. No output to file
    By shiva in forum Java Servlet
    Replies: 0
    Last Post: 04-24-2009, 02:06 PM
  3. Output to a text file
    By sfe23 in forum New To Java
    Replies: 4
    Last Post: 03-26-2009, 10:44 PM
  4. Replies: 7
    Last Post: 11-19-2008, 02:09 PM
  5. writting extended ascii chars on socket........or Endianness Issue......??
    By sachinj13 in forum Threads and Synchronization
    Replies: 8
    Last Post: 09-23-2008, 02:20 PM

Posting Permissions

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