Java NIO 读取文件、写入文件、读取写入混合

前言

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,目的是将有数据部分的字节,由缓冲写入到通道中。通常用在读与写之间。

读写文件代码

  1 package com.nio;
  2
  3 import java.io.*;
  4 import java.nio.ByteBuffer;
  5 import java.nio.channels.FileChannel;
  6 import java.nio.charset.Charset;
  7
  8 public class TestJavaNio {
  9
 10     public static String pathname = "d://test.txt";
 11
 12     @SuppressWarnings("resource")
 13     public static void main(String[] args) {
 14         readNIO();
 15         //writeNIO();
 16         //testReadAndWriteNIO();
 17     }
 18
 19     public static void readNIO() {
 20         //String pathname = "C:\\Users\\adew\\Desktop\\jd-gui.cfg";
 21         FileInputStream fin = null;
 22         try {
 23             fin = new FileInputStream(new File(pathname));
 24             FileChannel channel = fin.getChannel();
 25
 26             int capacity = 1000;// 字节
 27             ByteBuffer bf = ByteBuffer.allocate(capacity);
 28             System.out.println("限制是:" + bf.limit() + ",容量是:" + bf.capacity() + " ,位置是:" + bf.position());
 29             int length = -1;
 30
 31             while ((length = channel.read(bf)) != -1) {
 32
 33                 /*
 34                  * 注意,读取后,将位置置为0,将limit置为容量, 以备下次读入到字节缓冲中,从0开始存储
 35                  */
 36                 bf.clear();
 37                 byte[] bytes = bf.array();
 38                 System.out.println("start..............");
 39
 40                 String str = new String(bytes, 0 ,length);
 41                 System.out.println(str);
 42                 //System.out.write(bytes, 0, length);
 43
 44                 System.out.println("end................");
 45
 46                 System.out.println("限制是:" + bf.limit() + "容量是:" + bf.capacity() + "位置是:" + bf.position());
 47
 48             }
 49
 50             channel.close();
 51
 52         } catch (FileNotFoundException e) {
 53             e.printStackTrace();
 54         } catch (IOException e) {
 55             e.printStackTrace();
 56         } finally {
 57             if (fin != null) {
 58                 try {
 59                     fin.close();
 60                 } catch (IOException e) {
 61                     e.printStackTrace();
 62                 }
 63             }
 64         }
 65     }
 66
 67     public static void writeNIO() {
 68         String filename = "out.txt";
 69         FileOutputStream fos = null;
 70         try {
 71
 72             fos = new FileOutputStream(new File(filename));
 73             FileChannel channel = fos.getChannel();
 74             ByteBuffer src = Charset.forName("utf8").encode("你好你好你好你好你好");
 75             // 字节缓冲的容量和limit会随着数据长度变化,不是固定不变的
 76             System.out.println("初始化容量和limit:" + src.capacity() + ","
 77                     + src.limit());
 78             int length = 0;
 79
 80             while ((length = channel.write(src)) != 0) {
 81                 /*
 82                  * 注意,这里不需要clear,将缓冲中的数据写入到通道中后 第二次接着上一次的顺序往下读
 83                  */
 84                 System.out.println("写入长度:" + length);
 85             }
 86
 87         } catch (FileNotFoundException e) {
 88             e.printStackTrace();
 89         } catch (IOException e) {
 90             e.printStackTrace();
 91         } finally {
 92             if (fos != null) {
 93                 try {
 94                     fos.close();
 95                 } catch (IOException e) {
 96                     e.printStackTrace();
 97                 }
 98             }
 99         }
100     }
101
102     public static void testReadAndWriteNIO() {
103         String pathname = "C:\\Users\\adew\\Desktop\\test.txt";
104         FileInputStream fin = null;
105
106         String filename = "test-out.txt";
107         FileOutputStream fos = null;
108         try {
109             fin = new FileInputStream(new File(pathname));
110             FileChannel channel = fin.getChannel();
111
112             int capacity = 100;// 字节
113             ByteBuffer bf = ByteBuffer.allocate(capacity);
114             System.out.println("限制是:" + bf.limit() + "容量是:" + bf.capacity() + "位置是:" + bf.position());
115             int length = -1;
116
117             fos = new FileOutputStream(new File(filename));
118             FileChannel outchannel = fos.getChannel();
119
120
121             while ((length = channel.read(bf)) != -1) {
122
123                 //将当前位置置为limit,然后设置当前位置为0,也就是从0到limit这块,都写入到同道中
124                 bf.flip();
125
126                 int outlength = 0;
127                 while ((outlength = outchannel.write(bf)) != 0) {
128                     System.out.println("读," + length + "写," + outlength);
129                 }
130
131                 //将当前位置置为0,然后设置limit为容量,也就是从0到limit(容量)这块,
132                 //都可以利用,通道读取的数据存储到
133                 //0到limit这块
134                 bf.clear();
135
136             }
137         } catch (FileNotFoundException e) {
138             e.printStackTrace();
139         } catch (IOException e) {
140             e.printStackTrace();
141         } finally {
142             if (fin != null) {
143                 try {
144                     fin.close();
145                 } catch (IOException e) {
146                     e.printStackTrace();
147                 }
148             }
149             if (fos != null) {
150                 try {
151                     fos.close();
152                 } catch (IOException e) {
153                     e.printStackTrace();
154                 }
155             }
156         }
157     }
158
159 }

原文地址:https://www.cnblogs.com/cxxjohnson/p/9082567.html

时间: 2024-07-31 20:39:47

Java NIO 读取文件、写入文件、读取写入混合的相关文章

java nio通过ByteBuffer输出文件信息

1.通过ByteBuffer的get()方法每次读取一个字节转换成char类型输出. fc = new FileInputStream("src/demo20/data.txt").getChannel(); ByteBuffer buff = ByteBuffer.allocate(BSIZE); buff = ByteBuffer.allocateDirect(BSIZE); fc.read(buff); buff.flip(); while (buff.hasRemaining(

Java NIO FileVisitor 高效删除文件

在公司项目中,由于做个二维码扫码平台项目,预计每天产生的二维码图片达到十几G,所以要做个定时清理任务来定时清理图片,根据不同场景保留图片,规则是:1.二维码统一登录图片几个小时有效   2.电子名片二维码前几天有效,这些参数都是可配置的. 刚开始时,直接用Io 文件操作,递归删除文件,根据场景判断删除逻辑.但是,压力测试后发现,删除十几G文件要20分钟,直接测试不过.原因分析:在所有的5级目录下,要遍历匹配条件的文件,将不符合文件的删除,删除又用了递归,所有一下有三层循环,这样直接遍历数据太大,

Java NIO 利用通道完成文件复制(MappedByteBuffer)

相关学习网址: import java.io.IOException;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;import java.nio.channels.FileChannel.MapMode;import java.nio.file.Paths;import java.nio.file.StandardOpenOption; import org.junit.Test; @Testpubl

java nio 快速read大文件

If you want to make your first example faster FileChannel inChannel = new FileInputStream(fileName).getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(CAPACITY); while(inChannel.read(buffer) > 0) buffer.clear(); // do something with the data

Java NIO 应用 -- 使用内存映射文件实现进程间通信

一看到 Java NIO 的内存映射文件(MappedByteBuffer),让我立即就联想到 Windows 系统的内存映射文件.Windows 系统的内存映射文件能用来在多个进程间共享数据,即进程间的共享内存,是通过把同一块内存区域映射到不同进程的地址空间中,从而达到共享内存. Java NIO 的内存映射文件和 Windows 系统下的一样,都能把物理文件的内容映射到内存中,那么 MappedByteBuffer 是否能用来在不同 Java 进程(JVM) 间共享数据呢?答案是肯定的,这样

转:Java NIO(3)

要想讲清楚nio的原理和它的优点得先清楚Java应用程序的文件读写原理和虚拟内存的原理.Java文件读取原理可参见如下图: 当应用程序需要读取文件的时候,内核首先通过DMA技术将文件内容从磁盘读入内核中的buffer,然后Java应用进程再从内核的buffer将数据读取到应用程序的buffer. 为了提升I/O效率和处理能力,操作系统采用虚拟内存的机制.虚拟内存也就是我们常说的交换内存,它实际上是硬盘上的文件,虚拟内存有两个作用: 1. 不同的虚拟内存可以映射到相同的物理内存,根据这个原理,可以

Java NIO 完全学习笔记(转)

本篇博客依照 Java NIO Tutorial翻译,算是学习 Java NIO 的一个读书笔记.建议大家可以去阅读原文,相信你肯定会受益良多. 1. Java NIO Tutorial Java NIO,被称为新 IO(New IO),是 Java 1.4 引入的,用来替代 IO API的. Java NIO:Channels and Buffers 标准的 Java IO API ,你操作的对象是字节流(byte stream)或者字符流(character stream),而 NIO,你操

Java NIO 进程间通信

转自:http://blog.csdn.net/lingzhm/article/details/45026119 传统的进程间通信的方式有大致如下几种: (1)   管道(PIPE) (2)   命名管道(FIFO) (3)   信号量(Semphore) (4)   消息队列(MessageQueue) (5)   共享内存(SharedMemory) (6)   Socket Java如何支持进程间通信.我们把Java进程理解为JVM进程.很明显,传统的这些大部分技术是无法被我们的应用程序利

java nio读取和写入文件

读取 package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.text.MessageFormat; public class Test