Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to close PrintWriter?

807600Sep 6 2007 — edited Sep 6 2007
Hey guys I'm doing this program that prompts a user a menu and one of the options is to save a file that is already made in the same directory to a new file which hasn't been made. I dont have a problem doing so, but after it is done saving my original file keeps getting erased. here is some of the code and maybe one of you can help me.
  public void save() throws Exception
      {
			PrintWriter out=null;
			try{ out=new PrintWriter(new FileWriter(fName));}
			catch(IOException e){}
		
			try{ out= new PrintWriter(new FileWriter("outfile.txt"));}
			catch(IOException e){}
			for(int i=0; i<cCount; i++)
			{
				out.println(uWords[i]+" "+uCount);
}


System.out.println(fName+" is saving...");
System.out.println();
System.out.println(fName+" has been saved.");
System.out.println();
System.out.println();
out.close();

}//ends save method


The teacher says that the out.close() is important because if you don't close it the memory in the file will be lost, but as you can see i do close it, but am i closing it in the wrong place?

Thanks in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 4 2007
Added on Sep 6 2007
14 comments
718 views