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 & HelpOther Libraries › Creating, Writing, and Appending Files
Page Index Toggle Pages: 1
Creating, Writing, and Appending Files (Read 1721 times)
Creating, Writing, and Appending Files
Jan 7th, 2010, 12:03pm
 
I am using Processing to visualize data being received from an Arduino board over a standard serial port connection.  I have no problem getting the data to display in a Processing applet (using Interfascia) or sending serial commands back to the Arduino. What I would like to do is save this data to a file on the PC.  I have actually been able to do that, however I couldn't figure out how to open an existing file to append to it so I ended up making one file for each string of characters.  I can write whatever I want to one file while I have it open, but if I were to re-start the program or close the file, I wasn't able to append to it.  What would actually be perfect is something like the Xlsreader if it could write since my data is best saved as a spreadsheet. Any suggestions for updating or appending to an existing file?  The program would need to check if it exists - if not then create the file and write the data; if so then append it with new data.

Thanks!
Re: Creating, Writing, and Appending Files
Reply #1 - Jan 7th, 2010, 12:50pm
 
Code:
    String[] strings = { "One", "Two", "Three" };
String fileName = "D:/temp/P5_O.txt";
PrintWriter pw = null;
try
{
pw = new PrintWriter(new FileWriter(m_fileName, true));
for (int i = 0; i < strings.length; i++)
{
pw.println(strings[i]);
}
}
catch (IOException e)
{
e.printStackTrace(); // Dump and primitive exception handling...
}
finally
{
if (pw != null)
{
pw.close();
}
}

Warning: error checking is quite minimal in this code! To be improved...
Re: Creating, Writing, and Appending Files
Reply #2 - Jan 7th, 2010, 12:55pm
 
i guess thats the same question, PhiLho already answered there : http://processing.org/discourse/yabb2/?num=1251025598
Re: Creating, Writing, and Appending Files
Reply #3 - Jan 7th, 2010, 10:01pm
 
Ah, for once I don't point out to a previous thread!  Tongue
I wrote in that thread that there are several ways to do that, I shown here another one (closer of the way Processing writes lines in files).
Page Index Toggle Pages: 1