Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm copying a file from one location to another using java using the following code:
Java
InputStream fis = new FileInputStream(sourceFile);
OutputStream fos = new FileOutputStream(destFile);
byte[] databuf = new byte[1024];
int length;
while ((length = fis.read(databuf)) > 0) {

fos.write(databuf, 0, length);

}
fis.close();
fos.close();


But this program does not copy the file as fast as the windows explorer.
can anyone explain the reason?
is something wrong with my code or my approach?
Also suggest me if there is any other better(efficient) way of doing the same.
Posted

1 solution

Copying a File or Directory - The Java Tutorials[^]

That should kind of explain the action.

so I would use Files.copy(InputStream in, Path target, CopyOption... options) with the new Java7

or FileUtils.copyFile(new File("in.txt"), new File("out.txt")); from the lib commons I/O[^], which is anyway a pretty nice lib for file operations.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900