Deleting contents of a file before writing

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 2
Reputation: Joaoalex7 is an unknown quantity at this point 
Solved Threads: 0
Joaoalex7 Joaoalex7 is offline Offline
Newbie Poster

Deleting contents of a file before writing

 
0
  #1
Dec 27th, 2008
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:
  1. try
  2. {
  3. fW = new BufferedWriter(new FileWriter("Pedidos_Formacao.txt"));
  4. fW.write("Pedido");
  5. fW.newLine();
  6. fW.close;
  7. }

and this is for the object file:
  1. try
  2. {
  3. os = new ObjectOutputStream(new FileOutputStream("Utilizadores.bin"));
  4. User x = new User("João",1);
  5. os.writeObject(x);
  6. }

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Deleting contents of a file before writing

 
-1
  #2
Dec 27th, 2008
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!
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Deleting contents of a file before writing

 
0
  #3
Dec 27th, 2008
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();
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 2
Reputation: Joaoalex7 is an unknown quantity at this point 
Solved Threads: 0
Joaoalex7 Joaoalex7 is offline Offline
Newbie Poster

Re: Deleting contents of a file before writing

 
0
  #4
Dec 27th, 2008
You are right...stupid mistake by me...was calling the function in the wrong place

Anyway thank you all
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Deleting contents of a file before writing

 
0
  #5
Dec 27th, 2008
Mark solved threads as solved
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC