1. 基本 概念

- package sample;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.nio.ByteBuffer;
- import java.nio.channels.FileChannel;
- public class CopyFile {
- public static void main(String[] args) throws Exception {
- String infile = "C:\\copy.sql";
- String outfile = "C:\\copy.txt";
- // 获取源文件和目标文件的输入输出流
- FileInputStream fin = new FileInputStream(infile);
- FileOutputStream fout = new FileOutputStream(outfile);
- // 获取输入输出通道
- FileChannel fcin = fin.getChannel();
- FileChannel fcout = fout.getChannel();
- // 创建缓冲区
- ByteBuffer buffer = ByteBuffer.allocate(1024);
- while (true) {
- // clear方法重设缓冲区,使它可以接受读入的数据
- buffer.clear();
- // 从输入通道中将数据读到缓冲区
- int r = fcin.read(buffer);
- // read方法返回读取的字节数,可能为零,如果该通道已到达流的末尾,则返回-1
- if (r == -1) {
- break;
- }
- // flip方法让缓冲区可以将新读入的数据写入另一个通道
- buffer.flip();
- // 从输出通道中将数据写入缓冲区
- fcout.write(buffer);
- }
- }
- }
package sample; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class CopyFile { public static void main(String[] args) throws Exception { String infile = "C:\\copy.sql"; String outfile = "C:\\copy.txt"; // 获取源文件和目标文件的输入输出流 FileInputStream fin = new FileInputStream(infile); FileOutputStream fout = new FileOutputStream(outfile); // 获取输入输出通道 FileChannel fcin = fin.getChannel(); FileChannel fcout = fout.getChannel(); // 创建缓冲区 ByteBuffer buffer = ByteBuffer.allocate(1024); while (true) { // clear方法重设缓冲区,使它可以接受读入的数据 buffer.clear(); // 从输入通道中将数据读到缓冲区 int r = fcin.read(buffer); // read方法返回读取的字节数,可能为零,如果该通道已到达流的末尾,则返回-1 if (r == -1) { break; } // flip方法让缓冲区可以将新读入的数据写入另一个通道 buffer.flip(); // 从输出通道中将数据写入缓冲区 fcout.write(buffer); } } }

参数
|
写模式
|
读模式
|
position
|
当前写入的单位数据数量。
|
当前读取的单位数据位置。
|
limit
|
代表最多能写多少单位数据和容量是一样的。
|
代表最多能读多少单位数据,和之前写入的单位数据量一致。
|
capacity
|
buffer 容量
|
buffer 容量
|
场景
|
FileInputStream+
FileOutputStream
|
FileInputStream+
BufferedInputStream+
FileOutputStream
|
ByteBuffer+
FileChannel
|
MappedByteBuffer
+FileChannel
|
场景一时间 ( 毫秒 )
|
25155
|
17500
|
19000
|
16500
|
场景二时间 ( 毫秒 )
|
69000
|
67031
|
74031
|
71016
|
- package nio.readpage;
- import java.nio.ByteBuffer;
- import java.nio.channels.SocketChannel;
- import java.nio.charset.Charset;
- import java.net.InetSocketAddress;
- import java.io.IOException;
- public class BaiduReader {
- private Charset charset = Charset.forName("GBK");// 创建GBK字符集
- private SocketChannel channel;
- public void readHTMLContent() {
- try {
- InetSocketAddress socketAddress = new InetSocketAddress(
- "www.baidu.com", 80);
- //step1:打开连接
- channel = SocketChannel.open(socketAddress);
- //step2:发送请求,使用GBK编码
- channel.write(charset.encode("GET " + "/ HTTP/1.1" + "\r\n\r\n"));
- //step3:读取数据
- ByteBuffer buffer = ByteBuffer.allocate(1024);// 创建1024字节的缓冲
- while (channel.read(buffer) != -1) {
- buffer.flip();// flip方法在读缓冲区字节操作之前调用。
- System.out.println(charset.decode(buffer));
- // 使用Charset.decode方法将字节转换为字符串
- buffer.clear();// 清空缓冲
- }
- } catch (IOException e) {
- System.err.println(e.toString());
- } finally {
- if (channel != null) {
- try {
- channel.close();
- } catch (IOException e) {
- }
- }
- }
- }
- public static void main(String[] args) {
- new BaiduReader().readHTMLContent();
- }
- }
package nio.readpage; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.net.InetSocketAddress; import java.io.IOException; public class BaiduReader { private Charset charset = Charset.forName("GBK");// 创建GBK字符集 private SocketChannel channel; public void readHTMLContent() { try { InetSocketAddress socketAddress = new InetSocketAddress( "www.baidu.com", 80); //step1:打开连接 channel = SocketChannel.open(socketAddress); //step2:发送请求,使用GBK编码 channel.write(charset.encode("GET " + "/ HTTP/1.1" + "\r\n\r\n")); //step3:读取数据 ByteBuffer buffer = ByteBuffer.allocate(1024);// 创建1024字节的缓冲 while (channel.read(buffer) != -1) { buffer.flip();// flip方法在读缓冲区字节操作之前调用。 System.out.println(charset.decode(buffer)); // 使用Charset.decode方法将字节转换为字符串 buffer.clear();// 清空缓冲 } } catch (IOException e) { System.err.println(e.toString()); } finally { if (channel != null) { try { channel.close(); } catch (IOException e) { } } } } public static void main(String[] args) { new BaiduReader().readHTMLContent(); } }

