| | |
Deleting contents of a file before writing
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2007
Posts: 2
Reputation:
Solved Threads: 0
Hello
I'm having some trouble writing to text and object files in java. Basically i need to delete the contents of the file before i write the new information on it...which i'm not being able to do.
well this is what i have in terms of code for the text file:
and this is for the object file:
I have no problem writing but i need to delete the contents of the file before doing so or else this code will simply add the new information to the one that was already in the file.
Can anyone help me?
Thanks in advance
João
I'm having some trouble writing to text and object files in java. Basically i need to delete the contents of the file before i write the new information on it...which i'm not being able to do.
well this is what i have in terms of code for the text file:
java Syntax (Toggle Plain Text)
try { fW = new BufferedWriter(new FileWriter("Pedidos_Formacao.txt")); fW.write("Pedido"); fW.newLine(); fW.close; }
and this is for the object file:
java Syntax (Toggle Plain Text)
try { os = new ObjectOutputStream(new FileOutputStream("Utilizadores.bin")); User x = new User("João",1); os.writeObject(x); }
I have no problem writing but i need to delete the contents of the file before doing so or else this code will simply add the new information to the one that was already in the file.
Can anyone help me?
Thanks in advance
João
Hello,
Well this code will overwrite the data in file. You don't need to delete content of files before adding new information.
Also, i just tried both codes. Working perfectly! and data in both files were overwrote.
Best of luck!
Well this code will overwrite the data in file. You don't need to delete content of files before adding new information.
Also, i just tried both codes. Working perfectly! and data in both files were overwrote.
Best of luck!
Last edited by puneetkay; Dec 27th, 2008 at 2:56 pm.
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
•
•
Join Date: Sep 2008
Posts: 1,598
Reputation:
Solved Threads: 202
I would imagine that Puneetkay is correct since this is what I know from experience. But if you want to explicitly delete the file, use the File class.
http://java.sun.com/j2se/1.4.2/docs/...a/io/File.html
File myFile = new File("fileName.txt");
myFile.delete();
http://java.sun.com/j2se/1.4.2/docs/...a/io/File.html
File myFile = new File("fileName.txt");
myFile.delete();
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- "Trees" assignment... (C)
- links don't work in IE7 for top half of the page (HTML and CSS)
- Reading specific line in a file. / Searching (C++)
- Help with a reservation program! GUI Messed XD (Java)
- How do I get rid of xadsjt offeroptimizer? (Viruses, Spyware and other Nasties)
- help with database assignment (Java)
Other Threads in the Java Forum
- Previous Thread: Java Applet in another directory
- Next Thread: Help with GUI code!
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database defaultmethod development dice dragging ebook eclipse error event exception formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite input integer intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows







