IO流,文件复制

package 缓冲流;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

//文件的复制
public class MyCopy {
private  int size = 100;
private String src;
//构造方法重载,可以传如文件对象,也可以是路径
public MyCopy(String s){
File f=new File(s);
size=length(f);
src=s;
}
public MyCopy(File f) {
src=f.getAbsolutePath();
size=length(f);
}
public  void copy() {
try {
int index = src.lastIndexOf(".");
String dest = src.substring(0, index) + "-副本" + src.substring(index);
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
byte[] bs = new byte[size];
while (fis.available() > 0) {
fis.read(bs);
fos.write(bs);
int len = fis.available();
if (len < bs.length) {
    bs = new byte[len];
}
}
fos.flush();
fos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//计算文件大小,判断怎样大小的数组更有利于复制的速度;
public static int length(File f){
int t = 100;
long len=f.length();
//System.out.println(len);
if(len>10000000){
t=1000000;
}else if(len>1000000){
t=100000;
}else if(len>100000){
t=10000;
}else if(len>10000){
t=1000;
}
return t;

}
}

猜你喜欢

转载自qq-24665727.iteye.com/blog/2260072