`
- 浏览:
4548894 次
- 性别:
- 来自:
济南
-
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
相关推荐
一站式学习Java网络编程 全面理解BIO:NIO:AIO1
java源码:NIO网络框架 xSocket.rar
锂想系列12:NIO Day对于上游资源的启示.pdf
Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据...
适合于文件小但数量比较大的文件传输 传输速度比传统的流IO要快很多,刚接触nio不久,希望有朋友能对它再进行优化,相信很多项目里用的上
Maven坐标:org.jboss.xnio:xnio-nio:3.8.4.Final; 标签:jboss、xnio、nio、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...
java nio 写文件,java nio 写文件
java nio 读文件,java nio 读文件
NioSocket,包括server端和client端。server端有自动判定client掉线机制,client端有自动重连机制。本人已在项目实用,未经允许禁止转载!
nio-demo NIO编程demo 主要内容: 1、使用NIO实现文件读写,文件拷贝 2、使用NIO实现Socket编程 注:com.minghui.nio.demo包下是IBM官方demo
本类,是专门为了处理大文件,按行读取开发的类。 采用读文件的缓存 fbb 1024*5 行缓存 bb 256 字节 设计思想: 每次通过nio读取字节到 fbb中 然后对fbb自己中的内容进行行判断即 10 回车 13 行号 0 文件...
java NIO文件操作(中文版pdf),希望对大家有帮助,(转载)
NIO 处理大文件
java nio 写文件样例,java写大数据文件时提高性能
本类,是专门为了处理大文件,按行读取开发的类。 采用读文件的缓存 fbb 1024*5 行缓存 bb 256 字节 设计思想: 每次通过nio读取字节到 fbb中 然后对fbb自己中的内容进行行判断即 10 回车 13 行号 0 文件结束 ...
简单的用Java的NIO读取文件的程序,给大家参考。
Maven坐标:org.jboss.xnio:xnio-nio:3.8.0.Final; 标签:jboss、xnio、nio、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...
jsch-nio, 通过SSH实现 Java nio 文件系统 jsch-nio注意,这个项目依赖于 jsch扩展插件项目JSch是利用JSch实现实现一个 nio 文件系统,以及它随之一起运行的所有方法。 到目前为止,有一个功能齐全的unix/linux ...
Grizzly NIO框架旨在帮助开发人员利用Java:trade_mark:NIO API。 Grizzly的目标是帮助开发人员使用NIO构建可扩展且强大的服务器,并提供扩展的框架组件:Web框架(HTTP / S),WebSocket,Comet等!入门灰熊目前在...
依据 Doug Lea 的 基于 NIO 实现的 Reacotr 模式的回显服务器 BasicHandler: 单线程处理器 MultiReactor: 主从 Reactor MultithreadHandler: 线程池处理器 Reactor: 接收连接,I/O 读写 Reactor 模型的说明: ...