FileChannel 的拷贝性能,文件大小 39M 文件类型 test.zip 用时约 269ms
/*
2021-01-21 16:44:29.855 4247-4247/com.car.device I/copy_test: copyFile_2 start ....
2021-01-21 16:44:30.124 4247-4247/com.car.device I/copy_test: copyFile_2 end ....
public static void fileChannelCopy(File src,File dst) throws IOException {
FileChannel inChannel =new FileInputStream(src).getChannel();
FileChannel outChannel=new FileOutputStream(dst).getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inChannel.close();
outChannel.close();
}
*/
FileInputStream 的拷贝性能,文件大小 39M 文件类型 test.zip 用时约 5949ms
/*
2021-01-21 16:17:32.886 5107-5107/? I/copy_test: copyFile_2 start ....
2021-01-21 16:17:32.886 5107-5107/? I/copy_test: copyFile_2 11
2021-01-21 16:17:38.835 5107-5107/com.car.device I/copy_test: copyFile_2 复制完毕
2021-01-21 16:17:38.835 5107-5107/com.car.device I/copy_test: copyFile_2 end ....
public static void copyFile_2(String src, String dest) throws IOException {
Log.i("copy_test"," copyFile_2 11 ");
FileInputStream fis=new FileInputStream(src);
BufferedInputStream bis=new BufferedInputStream(fis);
FileOutputStream fos=new FileOutputStream(dest);
BufferedOutputStream bos=new BufferedOutputStream(fos);
//byte[] data=new byte[1024*10];
int len=-1;
while((len=bis.read())!=-1){
bos.write(len);
}
// System.out.println("复制完毕");
Log.i("copy_test"," copyFile_2 复制完毕 ");
bos.flush();
bis.close();
bos.close();
}
*/
/*
2021-01-21 16:12:57.966 4183-4183/com.car.device I/copy_test: copyFile_2 start ....
2021-01-21 16:12:57.966 4183-4183/com.car.device I/copy_test: copyFile 1
2021-01-21 16:12:58.033 4183-4183/com.car.device I/copy_test: copyFile 2
2021-01-21 16:12:58.437 4183-4183/com.car.device I/copy_test: copyFile 3
2021-01-21 16:12:58.437 4183-4183/com.car.device I/copy_test: copyFile_2 end ....
用时 441 ms
public static void copyFile(String src, String dest){
Log.i("copy_test"," copyFile 1 ");
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest))
){
Log.i("copy_test"," copyFile 2 ");
byte[] buffer = new byte[1024]; //数组大小可根据文件大小设置
int len = -1;
while ((len = bis.read(buffer)) != -1){ //读到文件末尾则返回-1
bos.write(buffer,0,len);
}
bos.flush();
Log.i("copy_test"," copyFile 3 ");
}catch (IOException e){
Log.i("copy_test"," copyFile IOException 4 " +e.toString());
e.getStackTrace();
}
}
*/
/*
2021-01-21 16:14:35.943 4406-4406/? I/copy_test: copyFile_2 start ....
2021-01-21 16:14:35.943 4406-4406/? I/copy_test: from path /storage/0006-76DF/test.zip
2021-01-21 16:14:35.943 4406-4406/? I/copy_test: to path /storage/0006-76DF/test/test.zip
2021-01-21 16:14:37.202 4406-4406/com.car.device I/copy_test: copy sucess
2021-01-21 16:14:37.212 4406-4406/com.car.device I/copy_test: del from path file
2021-01-21 16:14:37.222 4406-4406/com.car.device I/copy_test: copyFile_2 end ....
用时 37222 - 35943 = 1.279 秒
public boolean fileCopy_3(String from, String to) {
Log.i("copy_test","from path "+from );
Log.i("copy_test","to path "+to );
boolean result = false;
int size = 1 * 1024;
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(from);
out = new FileOutputStream(to);
byte[] buffer = new byte[size];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
result = true;
Log.i("copy_test","copy sucess " );
File delFile = new File(from);
if (delFile.exists()){
delFile.delete();
Log.i("copy_test","del from path file " );
}
} catch (FileNotFoundException e) {
Log.i("copy_test","del from path file " +e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.i("copy_test","del from path file " +e.toString());
e.printStackTrace();
} catch (Exception e) {
Log.i("copy_test","del from path file " +e.toString());
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
Log.i("copy_test","del from path file " +e.toString());
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
Log.i("copy_test","del from path file " +e.toString());
}
}
return result;
}
*/