原版(喽)
@Test
public void FileOutTest() {
String filePath="C:/Users/...../Desktop/123.jpeg";
String toPath="C:/Users/...../Desktop/123(1).png";
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream(filePath);
fos=new FileOutputStream(toPath);
byte[] bytes=new byte[1024];
while (true){
int len = fis.read(bytes);
if(len!=-1){
fos.write(bytes,0,len);
}else{
break;
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
新版(高效)
@Test
public void FileCache() {
try {
FileChannel fis = new FileInputStream(filePath).getChannel();
FileChannel fos = new FileOutputStream(toPath).getChannel();
ByteBuffer bb = ByteBuffer.allocate(1024 * 1024);
while (true) {
int read = fis.read(bb);
if (read == -1) {
break;
}
bb.flip();
fos.write(bb);
bb.clear();
}
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}