JAVA NIO

jdk1.4 开始引入了新IO类,NIO包的目的是为了提高IO的效率。

缓冲区Buffer:

缓冲区的优点我就不多介绍了。

拥有3个属性:

(1)容量:一个缓冲区最多容量。

(2)界限:一个缓冲区可读写的范围。

(3)位置:接下来要读写的位置。

界限是用来控制当前读写的范围,如果容量为100,界限为10,则位置只能在0-10之间,即只能读写0-10之间的数据

提供了三个方法改变属性:

(1)clear():把界限移到容量处,把位置设为0.

(2)flip():把界限移到位置处,把位置移到0.

(3)rewind():界限不变,位置设为0.

ByteBuffer:字节缓冲区

创建:ByteBuffer bf = ByteBuffer.allocate(capacity);

MappedByteBuffer:ByteBuffer的子类

能够把文件的部分映射到缓冲区中。
方法:

get();

put();

bb.isRemaining();判断是否还有数据可读

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class TransferDemo{
	public static void main(String args[])throws Exception{
		FileChannel in = new FileInputStream("test.txt").getChannel();
		FileChannel out = new FileOutputStream("out.txt").getChannel();
		ByteBuffer bf = ByteBuffer.allocate(1024);
		while(in.read(bf)!=-1){
			bf.flip();
			out.write(bf);
			bf.clear();
		}
		out.close();
		in.close();
	}
}

 

疑问:光有Buffer,但是怎么和数据源和数据汇进行连接呢?

Channel通道:把数据源或数据汇和Buffer连接起来。

FileChannel是一个实现好的类,是文件和缓冲区的通道,在FileInputStream、FileOutputStream、RandomAccessFile中的getChannel();获得。

(1)read(ByteBuffer bf);      读取文件数据到ByteBuffer中

(2)write(ByteBuffer bf);    把ByteBuffer写入文件

把String包装到ByteBuffer中:ByteBuffer.wrap (str.getBytes());

利用Charset能够对ByteBuffer进行编码转换。

Charset cs = Charset.forName("UTF-8");

CharBuffer cs.decode(ByteBuffer);把ByteBuffer解码为Unicode编码并返回。

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
public class FileChannelDemo01{
	public static void main(String args[])throws Exception{
		FileChannel fc = new FileOutputStream("text.txt").getChannel();
		fc.write(ByteBuffer.wrap("你好".getBytes()));
		fc.close();

		fc = new RandomAccessFile("text.txt","rw").getChannel();
		fc.position(fc.size());
		fc.write(ByteBuffer.wrap("朋友".getBytes()));
		fc.close();

		fc = new FileInputStream("text.txt").getChannel();
		ByteBuffer buf = ByteBuffer.allocate(1024);
		fc.read(buf);
		buf.flip();
		Charset cs = Charset.defaultCharset();
		System.out.println(cs.decode(buf));
		
		fc.close();
	}
}

 

Charset类:字符编码转换类

Charset cs = Charset.defaultCharset();

Charset cs = Charset.forName("UTF-8");

获得字符集

UTF-16BE是Unicode码。

ByteBuffer encode(String);把String按照指定的字符集编码,并返回。

CharBuffer decode(ByteBuffer)按照指定的字符集转换成Unicode码。

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class CharsetConvert{
	public static void main(String args[])throws Exception{
		ByteBuffer bb = ByteBuffer.wrap("你好".getBytes("UTF-8"));
		CharBuffer cb = bb.asCharBuffer();
		System.out.println(cb);
		bb = ByteBuffer.wrap("你好".getBytes("UTF-16BE"));
		cb = bb.asCharBuffer();
		System.out.println(cb);
		
	}
}

 

MappedByteBuffer:映射缓冲区

ByteBuffer的子类,用于因为文件太大不能放入内存的文件,能够映射文件的部分区域。

MappedByteBuffer b = channel.map(FileChannel.MapMode.READ_WRITE,pos,capacity);    pos表示文件开始映射位置,capacity表示要映射的容量。

 

import java.nio.*;
import java.io.*;
import java.nio.channels.*;
import java.nio.charset.*;
public class MappedByteBufferDemo01{
	public static void main(String args[])throws Exception{
		int capacity = 0x8000000;
		MappedByteBuffer mb = new RandomAccessFile("test.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE,0,capacity);
		mb.put("你好".getBytes("GBK"));
		mb.flip();
		System.out.println(Charset.forName("GBK").decode(mb)); 
	}
}

 

缓冲区视图:

ByteBuffer提供了一些方法可以得到其他的视图,比如asCharBuffer()获得CharBuffer。

(1)CharBuffer cb = bb.asCharBuffer();

(2)IntBuffer ib = bb.asIntBuffer();

ByteBuffer提供了

getInt()用于读取整数缓冲区视图put进去的整数。

getChar()用于读取字符缓冲区视图put进去的字符。

 

import java.nio.*;
public class BufferViewDemo{
	public static void main(String args[])throws Exception{
		ByteBuffer bb = ByteBuffer.allocate(100);
		CharBuffer cb = bb.asCharBuffer();
		cb.put("你好");
		bb.rewind();
		/*while(bb.hasRemaining()){
			System.out.println(bb.getChar()); 
		}*/
		IntBuffer ib = bb.asIntBuffer();
		int[] arr = {1,2,3};
		ib.put(arr);
		System.out.println(ib.get(2));
		ib.flip();
		while(ib.hasRemaining()){
			int i = ib.get();
			System.out.println(i); 
		}

	}
}

 

文件加锁:

得到锁并加锁:FileLock lock = channel.tryLock();

释放锁:lock.release();

 

 

Java NIO(New I/O)是Java 1.4引入的新的I/O API,用于替代标准的Java I/O API。它提供了非阻塞I/O操作,能显著提高程序的性能和可扩展性,尤其适用于处理大量并发连接的场景。 ### 核心组件 - **Channel(通道)**:Channel是对传统I/O中流的模拟,用于在缓冲区和实体(如文件、套接字)之间传输数据。常见的Channel实现有FileChannel、SocketChannel、ServerSocketChannel和DatagramChannel等。例如,FileChannel用于文件读写,SocketChannel用于TCP网络通信。 - **Buffer(缓冲区)**:Buffer是一个用于存储特定基本类型数据的容器。所有的缓冲区都是Buffer抽象类的子类,如ByteBuffer、CharBuffer、IntBuffer等。使用时,数据先被写入Buffer,再从Buffer读取到Channel,反之亦然。 - **Selector(选择器)**:Selector是Java NIO实现非阻塞I/O的关键。它允许一个线程处理多个Channel的I/O事件。通过将多个Channel注册到一个Selector上,Selector可以不断轮询这些Channel,当某个Channel有可用的I/O操作时,就会被Selector选中,从而实现单线程处理多个连接的目的。 - **SelectionKey(选择键)**:SelectionKey用于维护Selector和SelectableChannel的关系,每个Channel注册到Selector时都会产生一个SelectionKey,它聚合了Channel和Selector,有点类似EventKey。通过SelectionKey可以获取对应的Channel和Selector,还可以设置和查询感兴趣的I/O事件类型,如读、写、连接和接受连接事件等 [^1]。 ### 使用指南 #### 1. 使用FileChannel进行文件读写 ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class FileChannelExample { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("input.txt"); FileOutputStream fos = new FileOutputStream("output.txt"); FileChannel inChannel = fis.getChannel(); FileChannel outChannel = fos.getChannel()) { ByteBuffer buffer = ByteBuffer.allocate(1024); while (inChannel.read(buffer) != -1) { buffer.flip(); // 切换为读模式 outChannel.write(buffer); buffer.clear(); // 清空缓冲区,准备下一次写入 } } catch (IOException e) { e.printStackTrace(); } } } ``` #### 2. 使用Selector实现非阻塞网络编程 ```java import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.Set; public class NioServerExample { public static void main(String[] args) { try (ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); Selector selector = Selector.open()) { serverSocketChannel.socket().bind(new InetSocketAddress(8080)); serverSocketChannel.configureBlocking(false); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) { int readyChannels = selector.select(); if (readyChannels == 0) continue; Set<SelectionKey> selectedKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); while (keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); if (key.isAcceptable()) { ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel(); SocketChannel socketChannel = serverChannel.accept(); socketChannel.configureBlocking(false); socketChannel.register(selector, SelectionKey.OP_READ); } else if (key.isReadable()) { SocketChannel socketChannel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int bytesRead = socketChannel.read(buffer); if (bytesRead > 0) { buffer.flip(); byte[] data = new byte[buffer.remaining()]; buffer.get(data); System.out.println(new String(data)); } } keyIterator.remove(); } } } catch (IOException e) { e.printStackTrace(); } } } ``` ### 原理 Java NIO的非阻塞I/O原理基于操作系统的I/O多路复用机制。在传统的阻塞I/O模型中,一个线程只能处理一个连接,当线程在等待某个连接的数据时会被阻塞,无法处理其他连接。而在Java NIO中,Selector利用操作系统提供的I/O多路复用功能,如Linux的select、poll和epoll,通过一个线程监控多个Channel的I/O状态。当某个Channel有数据可读或可写时,Selector会感知到并通知应用程序进行相应的处理,从而实现单线程处理多个连接,提高了系统的并发处理能力。 ### 应用场景 - **网络编程**:在构建高性能的网络服务器时,如Web服务器、聊天服务器、游戏服务器等,Java NIO的非阻塞I/O特性可以显著减少线程数量,降低系统资源消耗,提高服务器的并发处理能力。 - **文件处理**:对于大文件的读写操作,使用FileChannel和ByteBuffer可以提高文件读写的效率,尤其是在需要随机访问文件内容时。 - **实时数据处理**:在处理实时数据流时,如视频流、音频流等,Java NIO可以高效地处理数据的传输和处理,确保数据的实时性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值