09-02
17
不同硬盘之间的高效率复制
作者:Java伴侣 日期:2009-02-17
复制内容到剪贴板 程序代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ReadFile {
private String sourceFileName;
private String sourcePathName;
private String aimFileName;
private String aimPathName;
/*public ReadFile(String sourcePathName,String sourceFileName,String aimPathName,String aimFileName ){
this.sourcePathName=sourcePathName;
this.sourceFileName=sourceFileName;
this.aimFileName=aimFileName;
this.aimPathName=aimPathName;
}*/
public void readWriteFile(String sourcePathName,String sourceFileName,String aimFileName,String aimPathName){
File aimFile=new File(aimPathName);
if(!aimFile.exists()){
if(!aimFile.mkdirs()) {
System.out.println("can not create file path!");
System.exit(1);
}
}
File aimDir=new File(aimFile,aimFileName);
File sourceFile=new File(sourcePathName);
if(!sourceFile.exists()){
System.out.println("the source file do not exist!Please check!");
System.exit(1);
}
File sourceDir=new File(sourceFile,sourceFileName);
FileOutputStream outputFile=null;
FileInputStream inputFile=null;
try{
inputFile=new FileInputStream(sourceDir);
outputFile=new FileOutputStream(aimDir,true);
}catch(FileNotFoundException e){
e.printStackTrace(System.err);
System.exit(1);
}
FileChannel inChannel=inputFile.getChannel();
FileChannel outChannel=outputFile.getChannel();
ByteBuffer buf=ByteBuffer.allocate(1024);
try{
while(inChannel.read(buf)!=-1){
System.out.print("source string is:"+((ByteBuffer)(buf.flip())).asCharBuffer().toString()+"");
outChannel.write(buf);
buf.clear();
}
outputFile.close();
System.out.println("EOF reached");
inputFile.close();
}catch(IOException e){
e.printStackTrace(System.err);
System.exit(1);
}
System.exit(0);
}
public static void main(String[] args){
String sourcePathName="E:\\sourceFile";
String sourceFileName="sFile.txt";
String aimPathName="D:\\aimFile";
String aimFileName="aFile.txt";
ReadFile rf=new ReadFile();
rf.readWriteFile(sourcePathName, sourceFileName, aimFileName, aimPathName);
}
}
评论: 0 | 引用: 0 | 查看次数: 337
发表评论