Writing to a File


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Writing to a File

  1. #1
    Join Date
    Oct 2005
    Posts
    36

    Writing to a File

    I need to write to a file without losing the contents of said file. What is the correct code? This is the code I'm using, but when I re-run the program, the file will lose its previous contents:

    Code:
    import java.io.*;
    class abc 
      {
         public static void main(String[] args) throws IOException
          {  
             BufferedReader a =new BufferedReader(new InputStreamReader(System.in));
             System.out.println("Please type something...");
             String b = a.readLine();
             PrintStream c = new PrintStream(new FileOutputStream("abc.txt"));
             c.println(b);
             c.close();
          }
      }
    Thanks.

  2. #2
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    try this, hope it helps,

    Code:
    try {
            BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
            out.write("aString");
            out.close();
        } catch (IOException e) {
        }

  3. #3
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    The FileOutputStream class has an overloaded constructor that you can use to set if you want to append or not. You can view it here.

  4. #4
    Join Date
    Oct 2005
    Posts
    36
    Thanks very much destin, it worked fine. I chose the FileOutputStream overloaded constructor you suggested.

  5. #5
    Join Date
    Oct 2005
    Posts
    36
    Why is it that when I use the new line character (\n), a small box is being created instead of going to a fresh line (using notepad)?

  6. #6
    Join Date
    Mar 2004
    Posts
    635
    I think thats just a notepad thing. Try opening in wordpad and see if appears differently.

  7. #7
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by Dreamer
    Why is it that when I use the new line character (\n), a small box is being created instead of going to a fresh line (using notepad)?
    I would recommend using PrintWriter rather than BufferedWriter. The PrintWriter class provides a println method which will print, with a new line after it; I'm pretty sure this eliminates the box problem.

  8. #8
    Join Date
    Oct 2005
    Posts
    36
    Thx for your quick reply. Actually I had switched to the FileWriter class, which I guess is similar to the PrintWriter one. However in Wordpad everything's ok, the problem lies with Notepad.

    Thx again guys.

Similar Threads

  1. Writing to a text file and setting the attributes
    By bcoates in forum VB Classic
    Replies: 4
    Last Post: 11-03-2005, 03:14 PM
  2. writing to a text file - VB 6 Please Help
    By sameera in forum VB Classic
    Replies: 1
    Last Post: 09-16-2005, 05:42 AM
  3. Replies: 2
    Last Post: 03-10-2005, 09:35 AM
  4. Replies: 2
    Last Post: 06-21-2003, 12:53 AM
  5. Replies: 0
    Last Post: 08-08-2002, 05:56 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center