public class Cpp {
private String sourceFile;
private String destFile;
public Cpp(String sourceFile,String destFile){
this.sourceFile = sourceFile;
this.destFile = destFile;
doCopy();
}
private void doCopy() {
FileInputStream in =null;
FileOutputStream out =null;
byte[] buffer = new byte[102400];
try {
in = new FileInputStream(this.sourceFile);
File dest = new File(this.destFile);
if(!dest.exists()){//目标文件对应的目录不存在,创建新的目录
int index = new String(this.destFile).lastIndexOf("/");
System.out.println(index);
String path = this.destFile.substring(0, index);
new File(path).mkdirs();
}
out = new FileOutputStream(this.destFile);
int num =0;
while((num=in.read(buffer))!=-1){
out.write(buffer,0,num);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Cpp.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException e){
Logger.getLogger(Cpp.class.getName()).log(Level.SEVERE, null, e);
} finally{
try {
if(in!=null)
in.close();
if(out!=null)
out.close();
} catch (IOException ex) {
Logger.getLogger(Cpp.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args){
String sourceFile = "c:/22.txt";
String destFile = "D:/das/a.txt";
long startTime =System.currentTimeMillis();
System.out.println("start to copy");
Cpp c= new Cpp(sourceFile,destFile);
long endTime = System.currentTimeMillis();
long time = (endTime-startTime)/1000;
System.out.println("copy end;cost:"+time);
}
private String sourceFile;
private String destFile;
public Cpp(String sourceFile,String destFile){
this.sourceFile = sourceFile;
this.destFile = destFile;
doCopy();
}
private void doCopy() {
FileInputStream in =null;
FileOutputStream out =null;
byte[] buffer = new byte[102400];
try {
in = new FileInputStream(this.sourceFile);
File dest = new File(this.destFile);
if(!dest.exists()){//目标文件对应的目录不存在,创建新的目录
int index = new String(this.destFile).lastIndexOf("/");
System.out.println(index);
String path = this.destFile.substring(0, index);
new File(path).mkdirs();
}
out = new FileOutputStream(this.destFile);
int num =0;
while((num=in.read(buffer))!=-1){
out.write(buffer,0,num);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Cpp.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException e){
Logger.getLogger(Cpp.class.getName()).log(Level.SEVERE, null, e);
} finally{
try {
if(in!=null)
in.close();
if(out!=null)
out.close();
} catch (IOException ex) {
Logger.getLogger(Cpp.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args){
String sourceFile = "c:/22.txt";
String destFile = "D:/das/a.txt";
long startTime =System.currentTimeMillis();
System.out.println("start to copy");
Cpp c= new Cpp(sourceFile,destFile);
long endTime = System.currentTimeMillis();
long time = (endTime-startTime)/1000;
System.out.println("copy end;cost:"+time);
}
6237

被折叠的 条评论
为什么被折叠?



