JAVA NIO学习
来源:互联网 发布:c语言源代码实例 编辑:程序博客网 时间:2024/06/05 22:37
貌似很久以前最初学习Java的时候顺带过学习了下NIO方面的,现在再看NIO根本不知道是什么东西了,这忘的,都不好意思说知道NIO了,虽然貌似说现在io.*里很多都有类似NIO的实现部分,不过去年做数据处理时还是发现阻塞式力不从心,目测以后会用到NIO的地方也会较多,提前复习+学习下,随便记录下代码做一记录,有时间的话再写写学习笔记好了。
/** * 基本的写 * @param filePath 文件路径 */public void write(String filePath) {FileOutputStream fout = null;try {String str = "NIO WRITE Test";byte[] message = str.getBytes();fout = new FileOutputStream(filePath);FileChannel fc = fout.getChannel();ByteBuffer buffer = ByteBuffer.allocate(1024);for (int i = 0; i < message.length; i++) {buffer.put(message[i]);}buffer.flip();fc.write(buffer);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {fout.close();} catch (IOException e) {e.printStackTrace();}}}
/** * 基本的读 * @param filePath 文件路径 */public void read(String filePath){FileInputStream fin = null;try {fin = new FileInputStream(filePath);FileChannel fc = fin.getChannel();ByteBuffer buffer = ByteBuffer.allocate(1024);fc.read(buffer);byte[] b = buffer.array();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}
/** * 文件拷贝 * @param from 源文件路径 * @param to 目标文件路径 */public void copy(String from,String to){FileInputStream fin = null;FileOutputStream fout = null;try {fin = new FileInputStream(from);fout = new FileOutputStream(to);FileChannel finChannel = fin.getChannel();FileChannel foutChannel = fout.getChannel();ByteBuffer buffer = ByteBuffer.allocate(1024);while(finChannel.read(buffer) != -1){buffer.flip();foutChannel.write(buffer);buffer.clear();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}其中flip()的使用和读写模式及Buffer的Capacity,Position,Limit有关,先Mark下。
0 0
- java.nio包学习
- Java Nio学习笔记
- java nio 缓冲区学习
- Java之nio学习
- java nio学习
- JAVA NIO学习
- Java NIO 学习
- Java NIO学习
- java NIO 入门学习
- JAVA NIO学习
- Java NIO学习笔记
- Java NIO 学习demo
- java nio学习资料
- java.nio-学习
- java NIO实例学习
- java nio学习(四)
- java NIO 学习笔记
- Java nio学习
- NSDate类用于保存时间值详解
- Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
- Learning Mysql -- 收集
- 移动设备的能源,未来无处不在
- node.js xml与json相互转化
- JAVA NIO学习
- 从零开始—我的开源项目5:代码完善(中)
- 如何删除适用于Firefox的 .NET Framework Assistant
- android-Service和Thread的区别
- Json字符串 与 java对象
- iOS的主要框架介绍
- django+python本地日志log源码
- json数据解析
- 未来的购物,智能商店时代来临