Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to copy and delete files?  RSS feed

 
Its Just Me
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are working with a specific OS you could use the OS commands by calling the Runtime.exec()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!