文件复制实现(已经测试)
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class Copy{
public static void main(String argv[]){
if(argv.length!=2){
System.out.println("usage>java Copy srcfilename destfilename");
System.exit(0);
}
try {
// Create channel on the source
FileChannel srcChannel = new FileInputStream(argv[0]).getChannel();
// Create channel on the destination
FileChannel dstChannel = new FileOutputStream(argv[1]).getChannel();
// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
// Close the channels
srcChannel.close();
dstChannel.close();
} catch (IOException e) {
}
}
}
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class Copy{
public static void main(String argv[]){
if(argv.length!=2){
System.out.println("usage>java Copy srcfilename destfilename");
System.exit(0);
}
try {
// Create channel on the source
FileChannel srcChannel = new FileInputStream(argv[0]).getChannel();
// Create channel on the destination
FileChannel dstChannel = new FileOutputStream(argv[1]).getChannel();
// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
// Close the channels
srcChannel.close();
dstChannel.close();
} catch (IOException e) {
}
}
}