We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › FileWriter function question
Page Index Toggle Pages: 1
FileWriter function question (Read 1298 times)
FileWriter function question
Nov 8th, 2009, 5:06am
 
When trying to use the following code snippet,  FileWriter is not writing to the "test.txt" file.  I inserted the "println("file updated") as a debug marker,  and that line is printing as expected,  but there is nothing being written to the test.txt

Here's the code:
Code:

FileWriter file;
 String test = "teststring";
  try  
   {  
 file = new FileWriter("test.txt", true); //bool tells to append
 file.write("\n"+test, 0, test.length()+1); //(string, start char, end char)
 file.close();
 println("file updated");
 }  
    catch(Exception e)  
  {  
    println("Error: Can't open file!");
   }


What am I doing wrong?
Re: FileWriter function question
Reply #1 - Nov 8th, 2009, 5:19am
 
the file is written but its written where the java applet is executed and thats somewhere in your temp files. so if you want the file to be saved somewhere else you have to declare an absolut path. for example
 file = new FileWriter("C:/test.txt", true);
Re: FileWriter function question
Reply #2 - Nov 8th, 2009, 5:41am
 
Ah,  would have never figured that one out.  

Thanks, Cedric....
Re: FileWriter function question
Reply #3 - Nov 8th, 2009, 6:25am
 
Ok,  it works like a champ,  but is there something I can do when writing the data to the .txt file,  but the file is not displayed as consecutive lines UNLESS it's opened in either Worpad or Word.  Notepad can not correctly act on /n parameter...  Is there any way,  using this format,  that the file will be displayed in the correct format in NOTEPAD ?  Not a real problem,  but just curious if it can be done...

Jim
Re: FileWriter function question
Reply #4 - Nov 8th, 2009, 6:43am
 
try "\r\n" and i would put it at the end so you dont have a first emtpy line.
Re: FileWriter function question
Reply #5 - Nov 8th, 2009, 6:44am
 
Yes, that's an old limitation of Notepad, the worst text editor ever made... Smiley
If you want to write lines the Windows way, use \r\n instead of \n.
Page Index Toggle Pages: 1