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 delete/rename a file
Page Index Toggle Pages: 1
How to delete/rename a file (Read 823 times)
How to delete/rename a file
Feb 23rd, 2008, 9:03am
 
My program creates temporary data files in the data directory.
Code:
String mfile=dataPath("mfile.txt");
PrintWriter output=createWriter(mfile);
..
output.println(num);
..
output.flush(); // Flush to file
output.close(); // Close file
How can my program delete them when no longer needed? Also how to rename a file?
Re: How to delete/rename a file
Reply #1 - Feb 24th, 2008, 10:24pm
 
I think you can use the Java.io.File class and its delete() and renameTo() methods. Have a look at the Java doc here :
http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html
Re: How to delete/rename a file
Reply #2 - Feb 24th, 2008, 11:12pm
 
I tried this Code:
File ff=new File("fnameA.txt");
ff.renameTo(new File(fnameB.txt"));
and this Code:
File ff=new File(dataPath("fnameA.txt"));
ff.renameTo(new File(dataPath("fnameB.txt")));
but all I got "File or directory does not exist.".

So I tried this Code:
String oldname=dataPath("fnameA.txt");
File ff=new File(oldname);
String newname=dataPath("fnameB.txt");
ff.renameTo(new File(newname));
and it work!
Page Index Toggle Pages: 1