Java中的IO和NIO是两种不同的输入输出处理方式,它们在设计理念、实现方式、性能特点和应用场景上有着显著的差异。
下面我将详细解释Java中的IO和NIO的区别,以及NIO如何实现多路复用,并提供一些日常开发中的使用建议和注意事项。
Java IO:面向流的处理方式,基于传统的阻塞式输入输出模型。数据以顺序的方式流动,且在读写过程中,一般情况下会阻塞当前线程,直到操作完成。
// Java IO示例:读取文件内容
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileIOExample {
public static void main(String[] args) {
String filePath = "example.txt";
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java NIO:面向缓冲区的处理方式,数据读取到一个缓冲区,需要时可在缓冲区中前后移动。这增加了处理过程中的灵活性。
// Java NIO示例:读取文件内容
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;
public class FileNIOExample {
public static void main(String[] args) {
String filePath = "example.txt";
try {
Files.lines(Paths.get(filePath)).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java IO:各种流是阻塞的,当一个线程调用read()或write()时,该线程被阻塞,直到数据传输完成。
// Java IO示例:阻塞式读取数据
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class BlockingIOExample {
public static void main(String[] args) {
try (InputStream inputStream = new FileInputStream("example.txt")) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
System.out.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java NIO:支持非阻塞模式,当一个通道没有数据可读时,线程可以继续处理其他事情,而不是阻塞在原地等待。
// Java NIO示例:非阻塞式读取数据
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.io.IOException;
public class NonBlockingNIOExample {
public static void main(String[] args) {
Path path = Paths.get("example.txt");
try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
int bytesRead;
while ((bytesRead = fileChannel.read(buffer)) != -1) {
buffer.flip();
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Java NIO示例:使用Selector实现多路复用
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;
public class NIOServer {
public static void main(String[] args) throws IOException {
Selector selector = Selector.open();
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(8080));
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
int readyChannels = selector.select();
if (readyChannels == 0) continue;
Iterator keyIterator = selector.selectedKeys().iterator();
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if (key.isAcceptable()) {
ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
SocketChannel clientChannel = serverChannel.accept();
clientChannel.configureBlocking(false);
clientChannel.register(selector, SelectionKey.OP_READ);
} else if (key.isReadable()) {
SocketChannel clientChannel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int bytesRead = clientChannel.read(buffer);
if (bytesRead > 0) {
buffer.flip();
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
buffer.clear();
}
}
keyIterator.remove();
}
}
}
}
NIO通过Selector实现多路复用,Selector允许一个线程同时监控多个Channel。当一个Channel有事件发生时,Selector会通知相应的线程进行处理。这就大大减少了线程的开销,让系统能够在高并发场景下保持高效。
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;
public class NIOServer {
public static void main(String[] args) throws IOException {
Selector selector = Selector.open();
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(8080));
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
int readyChannels = selector.select();
if (readyChannels == 0) continue;
Iterator keyIterator = selector.selectedKeys().iterator();
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if (key.isAcceptable()) {
ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
SocketChannel clientChannel = serverChannel.accept();
clientChannel.configureBlocking(false);
clientChannel.register(selector, SelectionKey.OP_READ);
} else if (key.isReadable()) {
SocketChannel clientChannel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int bytesRead = clientChannel.read(buffer);
if (bytesRead > 0) {
buffer.flip();
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
buffer.clear();
}
}
keyIterator.remove();
}
}
}
}
选择合适的IO模型:
合理使用缓冲区:
处理异常和资源释放:
避免过度优化:
通过理解Java中的IO和NIO的区别,以及NIO如何实现多路复用,开发者可以根据具体需求选择合适的IO模型,从而提高程序的性能和效率。