Hi, how do I copy and delete files? I read on this forum how to move a file by using the method renameTo(), but is not enough for me. Please help. Allard van Hooff.
Roy Ben Ami
Ranch Hand
Joined: Jan 13, 2002
Posts: 732
posted
0
delete is pretty easy. you have the delete() method in the File class u can use. to copy a file it is a bit more tricky (you can use the renameTo(File x) to rename it to the exact same name on a diffrent directory) or use this: import java.io.*; public class Ranch { public Ranch() { try{ FileInputStream in=new FileInputStream("your file here"); FileOutputStream out=new FileOutputStream("the copy file here"); byte buffer[]=new byte[4096]; int len; while((len=in.read(buffer))!=-1) out.write(buffer,0,len); in.close(); out.close(); } catch(IOException e){} } public static void main(String args[]) { Ranch x=new Ranch(); } }
Its Just Me
Greenhorn
Joined: Jan 10, 2002
Posts: 21
posted
0
sorry, I overlooked the delete()-method in the class File. But for the copying of files: is there no way arround reading a file and storing the exact same data in a different location? I was already doing this in my program (I read and store text files using BufferedReader and PrintWriter) but I thought there would be a more sophisticated way of copying files! So if somebody knows of another way of copying files, please let me know. AS for now, I'll stick to the read-and-store procedure. thanks.
Srinivas
Greenhorn
Joined: Feb 01, 2002
Posts: 9
posted
0
If you are working with a specific OS you could use the OS commands by calling the Runtime.exec()