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 › How to open a file for direct read/write access
Page Index Toggle Pages: 1
How to open a file for direct read/write access (Read 1486 times)
How to open a file for direct read/write access
Oct 5th, 2007, 4:12pm
 
I would like to know how can I open a file for a direct read/write access. Would I have to import a library, use pure java mode, or is it built in and I'm just dumb?
Re: How to open a file for direct read/write acces
Reply #1 - Oct 5th, 2007, 11:11pm
 
In processing you can use saveStrings() and loadStrings(). In java you could do something like the following:
Code:


//Writing to a file:
try{
//setting the second parameter in the FileWriter to true
//appends the text to what is already there. Otherwise it
//will overwrite the file.
BufferedWriter out = new BufferedWriter(new FileWriter("myfile.txt",true));
out.write("Hello again, ");
out.close();
} catch(IOException e) {
e.printStackTrace();
}

//reading from the file:
try{
BufferedReader in = new BufferedReader(new FileReader("myfile.txt"));
String s;
while((s = in.readLine()) != null){
println(s);
}
in.close();
}catch(IOException e){
e.printStackTrace();
}


Keep in mind that this works fine from processing but when you create an applet the applet will not have the permissions to read and write to the users computer. If you want this functionality you need to sign the applet. Check out toxi's post for how to do that http://processing.org/discourse/yabb/YaBB.cgi?board=Contributions_Beyond;action=display;num=1102415302#4
Hope this helps.
Page Index Toggle Pages: 1