`
qq_24665727
  • 浏览: 118065 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论
阅读更多
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;

}
}
6
1
分享到:
评论
1 楼 BS_YG 2015-11-29  
好用功啊,学习的榜样!不过length方法为何要定义成static,是为了上面的构造方法的调用吗,要是优化的话,把static去掉,上面直接改成this.length(),就行。

相关推荐

Global site tag (gtag.js) - Google Analytics