备份:nio文件读写

FileUtil .java



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;



/***/ /**
*
@authorlichun
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/

public class FileUtil ... {

publicstaticByteBufferreadFile(Stringfilename)throwsException
...{
FileChannelfiChannel
=newFileInputStream(filename).getChannel();
MappedByteBuffermBuf;
mBuf
=fiChannel.map(FileChannel.MapMode.READ_ONLY,0,fiChannel.size());
fiChannel.close();
fiChannel
=null;

returnmBuf;

}



publicstaticvoidsaveFile(ByteBuffersrc,Stringfilename)throwsException
...{
FileChannelfoChannel
=newFileOutputStream(filename).getChannel();
foChannel.write(src);
foChannel.close();
foChannel
=null;
}


publicstaticvoidsaveFile(FileChannelfiChannel,Stringfilename)throwsIOException
...{
MappedByteBuffermBuf;
mBuf
=fiChannel.map(FileChannel.MapMode.READ_ONLY,0,fiChannel.size());

FileChannelfoChannel
=newFileOutputStream(filename).getChannel();
foChannel.write(mBuf);

fiChannel.close();
foChannel.close();

fiChannel
=null;
foChannel
=null;
}



publicstaticvoidmain(String[]args)throwsException
...{
finalStringsuffix=".txt";
finalStringfilename="bufferTemp";
finalStringsrcFile=filename+suffix;
finalStringbackupFile=filename+"-"+System.currentTimeMillis()+suffix;
ByteBufferbyteBuffer
=FileUtil.readFile(srcFile);
FileUtil.saveFile(byteBuffer,backupFile);
byteBuffer
=null;

}

}


TransferFile.java

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util. * ;


/***/ /**
*
@authorlichun
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/

public class TransferFile ... {

privateStringsrcFile;

Listfiles
=newArrayList();
ByteBufferbyteBuffer;

publicTransferFile()
...{
createFileNames();
}


privatevoidcreateFileNames()
...{
for(inti=0;i<10;i++)
...{
files.add(i
+"");
}

}


publicListgetFiles()
...{
returnthis.files;
}


publicStringgetSrcFile()...{
returnsrcFile;
}


publicvoidsetSrcFile(StringsrcFile)...{
this.srcFile=srcFile;
}


publicvoidsaveFiles()throwsFileNotFoundException
...{
StringtempFile;
for(inti=0;i<this.files.size();i++)
...{
tempFile
="test-files/"+(String)files.get(i)+".txt";
System.out.println(
"begincreatethreadfor"+tempFile);

FileChannelfiChannel
=newFileInputStream(this.srcFile).getChannel();

WriteFileThreadwriteFileThread
=newWriteFileThread("writeFile",fiChannel,tempFile);
writeFileThread.start();
}


}


publicstaticvoidmain(String[]args)throwsException
...{
finalStringsrcFile="test-files/transferFile.txt";
TransferFiletransferFile
=newTransferFile();
transferFile.setSrcFile(srcFile);
transferFile.saveFiles();
}

}

WriteFileThread.java
import java.nio.channels.FileChannel;

/***/ /**
*
@authorlichun
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/

public class WriteFileThread extends Thread ... {

privateFileChannelfiChannel;
privateStringfileName;

publicWriteFileThread(Stringname)
...{
super(name);
}


publicWriteFileThread(Stringname,FileChannelfiChannel,StringfileName)
...{
this(name);
this.fiChannel=fiChannel;
this.fileName=fileName;
}


publicvoidsetFiChannel(FileChannelfiChannel)
...{
this.fiChannel=fiChannel;
}


publicFileChannelgetFiChannel()...{
returnfiChannel;
}


publicvoidsetFileName(StringfileName)...{
this.fileName=fileName;
}


publicStringgetFileName()...{
returnfileName;
}


publicvoidrun()
...{
System.out.println(
"inThread:"+this.getName());
try...{
FileUtil.saveFile(
this.fiChannel,this.fileName);
}
catch(Exceptione)...{
System.out.println(
"Thefileisnotfounded:"+fileName);
e.printStackTrace();
}

System.out.println(
"endThread:"+this.getName());
}

}

你可能感兴趣的:(java,thread)