import java.io.*;
public class FileUtils{
public static void copyFile(File in, File out) throws Exception {
FileInputStream fis = new FileInputStream(in);
FileOutputStream fos = new FileOutputStream(out);
try {
byte[] buf = new byte[1024];
int i = 0;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
}
catch (Exception e) {
throw e;
}
finally {
if (fis != null) fis.close();
if (fos != null) fos.close();
}
}
public static void main(String args[]) throws Exception{
FileUtils.copyFile(new File(args[0]),new File(args[1]));
}
}
[JDK1.4 using the java.nio package (faster)]
import java.io.*;
import java.nio.channels.*;
public class FileUtils{
public static void copyFile(File in, File out)
throws IOException
{
FileChannel inChannel = new
FileInputStream(in).getChannel();
FileChannel outChannel = new
FileOutputStream(out).getChannel();
try {
inChannel.transferTo(0, inChannel.size(),
outChannel);
}
catch (IOException e) {
throw e;
}
finally {
if (inChannel != null) inChannel.close();
if (outChannel != null) outChannel.close();
}
}
public static void main(String args[]) throws IOException{
FileUtils.copyFile(new File(args[0]),new File(args[1]));
}
}
ref : http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4643189
On the Windows plateform, you can't copy a file bigger than 64Mb, an Exception in thread "main" java.io.IOException: Insufficient system resources exist to complete the requested service is thrown.
For a discussion about this see : http://forum.java.sun.com/thread.jspa?threadID=439695&messageID=2917510
The workaround is to copy in a loop 64Mb each time until there is no more data.
Replace
...
try {
inChannel.transferTo(0, inChannel.size(),
outChannel);
}
... ...
try {
// magic number for Windows, 64Mb - 32Kb)
int maxCount = (64 * 1024 * 1024) - (32 * 1024);
long size = inChannel.size();
long position = 0;
while (position < size) {
position +=
inChannel.transferTo(position, maxCount, outChannel);
}
...Written and compiled by Réal Gagnon ©1998-2011
[ home ]