Results 1 to 10 of 10
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Question Emptying a Text File

    Hello!

    To print a text into a file you can do like this:

    Java Code:
    import java.io.*;
    
    public class tjena{
    	public static void main(String[] args){
    		try{
    			java.io.PrintStream ps = new java.io.PrintStream("hej.txt");
    			ps.println("Hello World!");
    			ps.close();
    		}catch(FileNotFoundException e){
    		}
    	}
    }
    However, this always empties the file of earlier contents. So how does one print into a text file without emptying it?

    Thanks in advance! :)

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,779
    Rep Power
    5

    Default Re: Emptying a Text File

    Check out the Java API's for file I/O. Some of the constructors allow opening for append.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Emptying a Text File

    I did what you said and found this code, but it doesn't open the file for append, nor does it output anything at all in the file or gives me a message that the file was not found.

    Java Code:
    import javax.swing.*;
    import java.io.*;
    
    public class tjena{
    	public static void main(String[] args){
    		try{
    			PrintStream ps = new PrintStream(new FileOutputStream("hej.txt", true));
    			ps.println("Hej!");
    			ps.close();
    		}catch(FileNotFoundException e){
    			JOptionPane.showMessageDialog(null, "File not found!");
    		}
    	}
    }

  4. #4
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Emptying a Text File

    This should help you out with what you are trying to do.

    How to append text to an existing file in Java - Stack Overflow

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,779
    Rep Power
    5

    Default Re: Emptying a Text File

    Well, that's because you didn't specify the correct file path. The above worked for me just fine.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Emptying a Text File

    But the program must have found the file, it would have told me otherwise because of the "FileNotFoundException". Or was that not what you meant?

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,779
    Rep Power
    5

    Default Re: Emptying a Text File

    Ah, Sorry. I misread your message. When you open a file for writing, it creates the file for you. If the file is already there, it opens it and writes to the end. What IDE are you using,if any?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Emptying a Text File

    I'm using Eclipse :)

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,779
    Rep Power
    5

    Default Re: Emptying a Text File

    So do I. It stores the file at the project level (i.e. inside the project). You may need to do a refresh to see it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Emptying a Text File

    Yeah, I found it :) Thanks for the help!

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  2. Replies: 2
    Last Post: 03-25-2011, 02:36 PM
  3. Replies: 7
    Last Post: 03-05-2011, 09:25 AM
  4. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  5. emptying/clearing an array
    By i8java in forum New To Java
    Replies: 2
    Last Post: 08-09-2010, 01:39 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
  •