Results 1 to 10 of 10
Thread: Emptying a Text File
- 10-02-2013, 05:54 PM #1
Senior Member
- Join Date
- Aug 2013
- Location
- Sweden
- Posts
- 163
- Rep Power
- 8
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){ } } }
Thanks in advance! :)
- 10-02-2013, 06:25 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Emptying a Text File
Check out the Java API's for file I/O. Some of the constructors allow opening for append.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-02-2013, 07:32 PM #3
Senior Member
- Join Date
- Aug 2013
- Location
- Sweden
- Posts
- 163
- Rep Power
- 8
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!"); } } }
- 10-02-2013, 08:36 PM #4
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
- 10-02-2013, 08:49 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-02-2013, 09:12 PM #6
Senior Member
- Join Date
- Aug 2013
- Location
- Sweden
- Posts
- 163
- Rep Power
- 8
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?
- 10-02-2013, 09:20 PM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-02-2013, 09:40 PM #8
Senior Member
- Join Date
- Aug 2013
- Location
- Sweden
- Posts
- 163
- Rep Power
- 8
Re: Emptying a Text File
I'm using Eclipse :)
- 10-02-2013, 11:18 PM #9
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-03-2013, 12:02 AM #10
Senior Member
- Join Date
- Aug 2013
- Location
- Sweden
- Posts
- 163
- Rep Power
- 8
Similar Threads
-
Reading from a text file, then writing back to Text Area in Reverse
By medic642 in forum New To JavaReplies: 8Last Post: 07-17-2011, 03:38 PM -
Read a data from a text file and create an object from these data in this text file
By jjavaa in forum Advanced JavaReplies: 2Last Post: 03-25-2011, 03:36 PM -
Transforming xml to text keeps placing blank line at beginning of text file
By DerekRaimann in forum Advanced JavaReplies: 7Last Post: 03-05-2011, 10:25 AM -
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 06:56 PM -
emptying/clearing an array
By i8java in forum New To JavaReplies: 2Last Post: 08-09-2010, 02:39 PM
Bookmarks