• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to copy and delete files?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are working with a specific OS you could use the OS commands by calling the Runtime.exec()
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic