dcsimg
How to copy or move a file using java??
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Melvin_John
Posted On:   Friday, June 16, 2006 10:20 PM

Hi,
i'm really a newbi in java.I want to knw the code for how to 1)copy files using java
2)move files using java

thanks for u'r reply's in advance,
vinny

Re: How to copy or move a file using java??

Posted By:   Anonymous  
Posted On:   Wednesday, July 5, 2006 07:44 AM


import java.io.*;
public class CopyFile
{
public static void main(String args[]) throws Exception
{
BufferedReader br = new BufferedReader(
new FileReader("line.txt"));
BufferedWriter bw = new BufferedWriter(
new FileWriter("linenum.txt"));
String s, space=" ";
int num=0;
while (br.ready())
{
s=br.readLine();
num++;
bw.write(String.valueOf(num));
bw.write(space);
bw.write(s);
bw.newLine();
}
bw.close();
}
}

More details see here

http://www.developerzone.biz/index.php?option=com_content&task=view&id=67&Itemid=36&PHPSESSID=36a58617c5b15fceb30cdcd23e8294fa

Re: How to copy or move a file using java??

Posted By:   Anonymous  
Posted On:   Tuesday, June 20, 2006 12:48 AM

Or use Ant's task. You could use Ant as a class library, too.

Re: How to copy or move a file using java??

Posted By:   Robert_Lybarger  
Posted On:   Saturday, June 17, 2006 09:11 AM

Moving a file can be done via the "renameTo(File)" method call on a File object. Unfortunately Java versions up to this point do not provide a simple "copyTo" API call to copy a file. I think I heard a rumor that it will finally be provided in "mustang" (java 6?) release, but that is still a little ways off. This has been a minor thorn in the Java community as a result (the old "its system dependent" cop out). Anyway, you can open a new file yourself (createNewFile()), open a reader on the source file and a write on the target file, and transfer the bits over yourself with some [buffered] read/writes. Doing the reads/writes buffered is definitely the way to go, but even so, the algorithms for doing this quickly and efficiently are part artform and part religion. You can get a poor-man's version using just something like while((line=reader.readLine())!=null) { writer.write(line+"
"); }

Re: How to copy or move a file using java??

Posted By:   WarnerJan_Veldhuis  
Posted On:   Saturday, June 17, 2006 05:10 AM

About | Sitemap | Contact