FileUtil .java


importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.nio.ByteBuffer;
importjava.nio.MappedByteBuffer;
importjava.nio.channels.FileChannel;



/***//**
*@authorlichun
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
publicclassFileUtil...{
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;
}
}

importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.nio.ByteBuffer;
importjava.nio.channels.FileChannel;
importjava.util.*;


/***//**
*@authorlichun
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
publicclassTransferFile...{
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();
}
}
importjava.nio.channels.FileChannel;

/***//**
*@authorlichun
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
publicclassWriteFileThreadextendsThread...{
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());
}
}
126

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



