62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class FileCopy {
public static void main(String[] args) {
File fromFile = new File("F:\\234.txt");
if(!fromFile.exists()) {
System.out.printf("File to copy, %s, does not exist.",
fromFile.getAbsolutePath());
System.exit(1);
}
File toFile = new File("D:\\565.txt");
FileInputStream inFile = null;
FileOutputStream outFile = null;
try {
inFile = new FileInputStream(fromFile);
outFile = new FileOutputStream(toFile);
} catch(FileNotFoundException e) {
e.printStackTrace(System.err);
assert false;
}
FileChannel inChannel = inFile.getChannel();
FileChannel outChannel = outFile.getChannel();
try {
int bytesWritten = 0;
long byteCount = inChannel.size();
while(bytesWritten<byteCount) {
bytesWritten += inChannel.transferTo(bytesWritten,
byteCount-bytesWritten,
outChannel);
}
System.out.printf("File copy complete. %d bytes copied to %s%n",
byteCount, toFile.getAbsolutePath());
inFile.close();
outFile.close();
} catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
System.exit(0);
}
}