标签:NIO 示例 buffer FileChannel read file new channel
1. 写文件操作
/** * 写文件 */ public class FileChannelTest { public static void main(String[] args) throws IOException { String str = "test file channel, 测试file channel"; // 创建一个输出流 FileOutputStream fileOutputStream = new FileOutputStream("D://nio.txt"); // 得到file channel FileChannel fileChannel = fileOutputStream.getChannel(); // 创建一个缓存buffer ByteBuffer byteBuffer = ByteBuffer.allocate(1024); // 将数据放到buffer中 byteBuffer.put(str.getBytes()); // 反转, read -> write byteBuffer.flip(); // 将buffer中的数据写入到file channel fileChannel.write(byteBuffer); fileChannel.close(); } }
2. 读文件操作
/** * 读文件 */ public class FileChannelTest02 { public static void main(String[] args) throws Exception { File file = new File("D://nio.txt"); // 创建一个输入流 FileInputStream fileInputStream = new FileInputStream(file); // 得到file channel FileChannel fileChannel = fileInputStream.getChannel(); // 创建一个缓存buffer ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length()); // 将file channel 中的数据读入到buffer中 fileChannel.read(byteBuffer); byte[] array = byteBuffer.array(); String data = new String(array); System.out.println(data); fileChannel.close(); } }
3. 使用FileChannel拷贝文件
/** * 文件的拷贝,用一个buffer完成的读写 */ public class CopyFileTest03 { public static void main(String[] args) throws Exception { FileInputStream fileInputStream = new FileInputStream("src/1.txt"); FileChannel channel = fileInputStream.getChannel(); FileOutputStream fileOutputStream = new FileOutputStream("2.txt"); FileChannel channel1 = fileOutputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); /** * 下面的代码存在bug , 只会读写一次,如果文件内容大于1024, 后面的内容就会丢失 */ // // 将channel中的数据读到buffer中 // channel.read(buffer); // buffer.flip(); // // 将缓冲区的数据读取到通道中 // channel1.write(buffer); while (true) { // 如果调用clear()方法,会出现死循环, read == 0 . buffer.clear(); int read = channel.read(buffer); System.out.println(read); if (read == -1) { break; } // 数据从buffer中写到channel中 buffer.flip(); channel1.write(buffer); } fileInputStream.close(); fileOutputStream.close(); } }
4. 调用FileChannel的API完成文件拷贝
/** * 使用transferFrom 和 transferTo 拷贝文件 * */ public class CopyFileTest04 { public static void main(String[] args) throws Exception { FileInputStream fileInputStream = new FileInputStream("1.jpg"); FileChannel inputStreamChannel = fileInputStream.getChannel(); FileOutputStream fileOutputStream = new FileOutputStream("2.jpg"); FileChannel outputStreamChannel = fileOutputStream.getChannel(); // 文件拷贝 // outputStreamChannel.transferFrom(inputStreamChannel, 0, inputStreamChannel.size()); inputStreamChannel.transferTo(0, inputStreamChannel.size(), outputStreamChannel); // 关闭通道和流 fileInputStream.close(); fileOutputStream.close(); } }
注意:
读写同一个buffer时,需要flip();
读写1次buffer之后,需要clear(), 将buferr复位
标签:NIO,示例,buffer,FileChannel,read,file,new,channel 来源: https://www.cnblogs.com/z-qinfeng/p/11962716.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。