`
ihuashao
  • 浏览: 4548894 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

备份:nio文件读写

阅读更多

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;

}

}


TransferFile.java

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();
}

}

WriteFileThread.java
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());
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics