JAVA 文件IO简单总结
来源:互联网 发布:阿里云带宽虚标 编辑:程序博客网 时间:2024/06/06 07:39
1) I/O
读写流,一定是Blocking的。
效率较好的用法是,使用Buffer。例如BufferReader,每次先读取指定大小的内容到Buffer中,而不是每次读取一个字节
读取文件代码例:
String line = null;try(BufferedReader reader = new BufferedReader(new FileReader("filename"))) {;while ((line = reader.readLine()) != null) {// do something}} catch (IOException e) {e.printStackTrace();}
Stream的家族十分庞大,下图是搜索到的一个较全的概括
另附PrintWriter和Filewriter的对比:
http://stackoverflow.com/questions/5759925/printwriter-vs-filewriter-in-java
2)NIO
※使用FileChannel读取文件,无法按行读取,但是可以获取/指定当前位置(Position)
RandomAccessFile aFile = new RandomAccessFile("filename", "r"); FileChannel inChannel = aFile.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while(inChannel.read(buffer) > 0) { // 转换到读取模式 buffer.flip(); // 处理Buffer // clear或者compact buffer.clear(); } inChannel.close(); aFile.close();
与read对应的Write方法负责写入
ByteBuffer buf = ByteBuffer.allocate(48);buf.clear();buf.put(newData.getBytes());buf.flip();while(buf.hasRemaining()) { channel.write(buf);}
※读取大文件,可以使用内存映射的MapperedByteBuffer,内存映射不直接将文件读入物理内存
RandomAccessFile aFile = new RandomAccessFile("filename", "r");FileChannel inChannel = aFile.getChannel();MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());buffer.load();// Do Somethingbuffer.clear();inChannel.close();aFile.close();
※NIO提供Path接口代替File类,并且提供Paths、Files工具类方便操作
例如,读取小文件的所有内容,只需以下步骤:
Path path = Paths.get("filepath");new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
fileChannel.read方法返回一个Future,在执行完成后operation.isDone()将会返回true
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);ByteBuffer buffer = ByteBuffer.allocate(1024);long position = 0;Future<Integer> operation = fileChannel.read(buffer, position);while(!operation.isDone());buffer.flip();byte[] data = new byte[buffer.limit()];buffer.get(data);System.out.println(new String(data));buffer.clear();
fileChannel.read(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(Integer result, ByteBuffer attachment) { System.out.println("result = " + result); attachment.flip(); byte[] data = new byte[attachment.limit()]; attachment.get(data); System.out.println(new String(data)); attachment.clear(); } @Override public void failed(Throwable exc, ByteBuffer attachment) { }});
参考文章:
http://blog.csdn.net/yczz/article/details/38761237
http://www.cnblogs.com/xubenben/p/4424398.html
http://tutorials.jenkov.com/java-nio/index.html
0 0
- JAVA 文件IO简单总结
- java文件IO总结
- Java IO 8:IO简单总结
- java IO流简单总结
- Java 简单IO文件处理
- JAVA IO 简单读写文件
- java io文件操作总结
- java IO总结之文件
- java中io流简单总结
- JAVA IO流简单读写文件
- Java中IO流操作文件总结
- java io文件读写的总结
- Java IO字节流读取文件总结
- Java IO字符流读取文件总结
- java的文件IO编程知识总结
- Java中IO流操作文件总结
- Java文件IO的使用总结
- Java IO总结(一) File文件类
- Linux 下安装openstf
- python反射机制深入分析
- 动态包含(<jsp:include>)
- Oracle创建触发器详解及例子
- Java中Synchronized的用法
- JAVA 文件IO简单总结
- SQL语句跟部分执行顺序
- 取消ListView的点击效果
- 完美解决安卓滑动删除 以及下拉刷新的事件冲突
- linux运维经验总结
- linux 修改的文件在掉电时会损坏的分析与处理
- virtio相关
- 织梦编辑器换百度编辑器
- 圆形imageView控件