File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes How to copy and delete files? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "How to copy and delete files?" Watch "How to copy and delete files?" New topic
Author

How to copy and delete files?

Its Just Me
Greenhorn

Joined: Jan 10, 2002
Posts: 21
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
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
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
If you are working with a specific OS you could use the OS commands by calling the Runtime.exec()


Srinivas
 
 
subject: How to copy and delete files?
 
Threads others viewed
Java file rename/move and inherited permissions
FIle.delete()
creating a link
Enumerate FTP directory?
About deleting files using listFiles()

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture