Java NIO 读取文件、写入文件、读取写入混合详解编程语言
2023-06-13 09:11:53 时间
前言
Java NIO(new/inputstream outputstream)使用通道、缓冲来操作流,所以要深刻理解这些概念,尤其是,缓冲中的数据结构(当前位置(position)、限制(limit)、容量(capacity)),这些知识点要通过写程序慢慢体会。
NIO vs 传统IO
NIO是面向缓冲、通道的;传统IO面向流
通道是双向的既可以写、也可以读;传统IO只能是单向的
NIO可以设置为异步;传统IO只能是阻塞,同步的
缓冲区结构图
NIO是面向缓冲区的,缓冲区可以理解为一块内存,有大小。缓冲区有位置、界限、容量几个概念。
capacity:容量,缓冲区的大小
limit:限制,表示最大的可读写的数量
position:当前位置,每当读写,当前位置都会加一
flip和clear方法,内部就操作这三个变量。
缓冲区常用方法
clear:将当前位置设置为0,限制设置为容量,目的是尽最大可能让字节,由通道读取到缓冲中
flip:当前位置置为限制,然后将当前位置置为0,目的是将有数据部分的字节,由缓冲写入到通道中。通常用在读与写之间。
读写文件代码
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; public class TestJavaNIO { static void readNIO() { String pathname = "C://Users//adew//Desktop//jd-gui.cfg"; FileInputStream fin = null; try { fin = new FileInputStream(new File(pathname)); FileChannel channel = fin.getChannel(); int capacity = 100;// 字节 ByteBuffer bf = ByteBuffer.allocate(capacity); System.out.println("限制是:" + bf.limit() + "容量是:" + bf.capacity() + "位置是:" + bf.position()); int length = -1; while ((length = channel.read(bf)) != -1) { * 注意,读取后,将位置置为0,将limit置为容量, 以备下次读入到字节缓冲中,从0开始存储 bf.clear(); byte[] bytes = bf.array(); System.out.write(bytes, 0, length); System.out.println(); System.out.println("限制是:" + bf.limit() + "容量是:" + bf.capacity() + "位置是:" + bf.position()); channel.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fin != null) { try { fin.close(); } catch (IOException e) { e.printStackTrace(); static void writeNIO() { String filename = "out.txt"; FileOutputStream fos = null; try { fos = new FileOutputStream(new File(filename)); FileChannel channel = fos.getChannel(); ByteBuffer src = Charset.forName("utf8").encode("你好你好你好你好你好"); // 字节缓冲的容量和limit会随着数据长度变化,不是固定不变的 System.out.println("初始化容量和limit:" + src.capacity() + "," + src.limit()); int length = 0; while ((length = channel.write(src)) != 0) { * 注意,这里不需要clear,将缓冲中的数据写入到通道中后 第二次接着上一次的顺序往下读 System.out.println("写入长度:" + length); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); static void testReadAndWriteNIO() { String pathname = "C://Users//adew//Desktop//test.txt"; FileInputStream fin = null; String filename = "test-out.txt"; FileOutputStream fos = null; try { fin = new FileInputStream(new File(pathname)); FileChannel channel = fin.getChannel(); int capacity = 100;// 字节 ByteBuffer bf = ByteBuffer.allocate(capacity); System.out.println("限制是:" + bf.limit() + "容量是:" + bf.capacity()+ "位置是:" + bf.position()); int length = -1; fos = new FileOutputStream(new File(filename)); FileChannel outchannel = fos.getChannel();
//将当前位置置为limit,然后设置当前位置为0,也就是从0到limit这块,都写入到同道中 bf.flip(); int outlength =0; while((outlength=outchannel.write(bf)) != 0){ System.out.println("读,"+length+"写,"+outlength); //将当前位置置为0,然后设置limit为容量,也就是从0到limit(容量)这块, //都可以利用,通道读取的数据存储到 //0到limit这块 bf.clear(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fin != null) { try { fin.close(); } catch (IOException e) { e.printStackTrace(); if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); @SuppressWarnings("resource") public static void main(String[] args) { testReadAndWriteNIO();
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20279.html
cjava相关文章
- java解析xml方法_详解Java解析XML的四种方法
- java局域网发送文件_Java如何实现局域网文件传输代码案例分享
- java 删除目录下所有文件_Java删除文件、目录及目录下所有文件的方法实例
- java工具类-Java对象转换成Map
- java反转数组_Java实现数组反转翻转的方法实例
- java删除文件目录及文件_Java删除文件,目录
- Java文件是如何编译成Dex文件的?如何执行Dex文件?「建议收藏」
- 做了三年Java,java注释的三种形式
- java源程序文件扩展名_JAVA源代码的扩展名为( )
- 如何在eclipse中导入java工程文件和java源代码文件
- MySQL字段类型如何转为java_Java JDBC中,MySQL字段类型到JAVA类型的转换
- java 读取字符串文件_Java读取文件为字符串
- java判断文件是否为图片格式_java读取图片流
- Java读取Properties文件的六种方法 .详解编程语言
- maven java读取maven的pom.xml文件详解编程语言
- Java 使用NIO进行快速的文件拷贝的代码详解编程语言
- Java多线程实现文件快速切分详解编程语言
- 使用java NIO进行读文件详解编程语言
- Java 使用 NIO 进行文件合并输出详解编程语言
- 技术的融合突破极限:Java与Redis的技术融合(java与redis)
- 快速上手:Java连接Mongodb数据库(java连接mongodb数据库)
- 连接MySQL连接技术之Java实现(java的mysql)
- Linux下编译Java文件,迎来新体验(linux编译java文件)
- 深入分析java文件路径的详解
- 利用java操作Excel文件的方法
- java压缩多个文件并且返回流示例
- java反射机制示例