点击(此处)折叠或打开
-
public void fileChannelCopy(File s, File t) {
-
-
FileInputStream fi = null;
-
-
FileOutputStream fo = null;
-
-
FileChannel in = null;
-
-
FileChannel out = null;
-
-
try {
-
-
fi = new FileInputStream(s);
-
-
fo = new FileOutputStream(t);
-
-
in = fi.getChannel();//得到对应的文件通道
-
-
out = fo.getChannel();//得到对应的文件通道
-
-
in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
-
-
} catch (IOException e) {
-
-
e.printStackTrace();
-
-
} finally {
-
-
try {
-
-
fi.close();
-
-
in.close();
-
-
fo.close();
-
-
out.close();
-
-
} catch (IOException e) {
-
-
e.printStackTrace();
-
-
}
-
-
}
-
- }
以文件缓冲区的方式进行文件复制
点击(此处)折叠或打开
-
private boolean copyFile (String fileFrom, String fileTo) {
-
try {
-
FileInputStream in = new java.io.FileInputStream(fileFrom);
-
FileOutputStream out = new FileOutputStream(fileTo);
-
byte[] bt = new byte[1024];
-
int count;
-
while ((count = in.read(bt)) > 0) {
-
out.write(bt, 0, count);
-
}
-
in.close();
-
out.close();
-
return true;
-
} catch (IOException ex) {
-
return false;
-
}
- }