传统I/O
- 输入输出概念:这个输入输出相对于内存为主体,输入就是内存从文件中拿数据,输出就是内存从文件中写数据。
- 操作字节流:FileOutputStream,FileInputStream
- 操作字符流:Writer,Reader
- 嵌套各种流(例如缓冲,字节流转字符流等):BufferOutputStream,InputStreamReader
-
相关实例:
io1()方法就是对字节流进行操作。
io2()方法就是字节流转成字符流,让后套上Bufferedxxxx给流加上缓存。
io3()方法就是给输出流加上Buffered缓存,对于输出流加上缓存,需要在最后flush,把数据都写出去,对于输入流加上缓存,是每次多读⼀些放在内存⾥⾯,下次再取数据就不⽤再和外部做交互(即不必做 IO 操作)。
io3()方法就是一个文件复制的操作
private static void io1() {
try (OutputStream outputStream = new FileOutputStream("./text.txt")) {
outputStream.write('a');
outputStream.write('b');
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void io2() {
try (InputStream inputStream = new FileInputStream("./text.txt")) {
Reader reader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
System.out.println(bufferedReader.readLine());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void io3() {
try (OutputStream outputStream = new FileOutputStream("./19_io/text.txt");
Writer writer = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
bufferedWriter.write('a');
bufferedWriter.write('b');
bufferedWriter.flush(); //可以去掉,原因是在try中创建BufferedWriter,关闭时会自动flush,如果在中途使用就要手动flush
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void io4() {
try (InputStream inputStream = new FileInputStream("./text.txt");
OutputStream outputStream = new FileOutputStream("./new_text.txt")) {
byte[] data = new byte[1024];
int read;
while ((read = inputStream.read(data)) != -1) {
outputStream.write(data, 0, read);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
NIO
NIO 和 IO 的区别:
- 传统IO用的是插管道的方式,用的是Stream;NIO用的也是插管道的方式,用的是Channel。NIO 的 Channel 是双向的。
- NIO 也用到 buffer,buffer也是与传统IO最大的区别:它的Buffer可以被操作,它强制使用 Buffer。
- NIO 有非阻塞式的⽀持
只是支持非阻塞式,而不是全是非阻塞式。默认是阻塞式的
而且就算是非阻塞式,也只是网络交互支持,⽂件交互是不支持的
Buffer使用
capacity: 缓冲区数组的总长度
position: 游标位置,下一个要操作的数据元素的位置
limit: 限制位置,缓冲区数组中不可操作的下一个元素的位置,limit<=capacity
mark: 用于记录当前 position 的前一个位置或者默认是 0
当ByteBuffer.allocate(1024)时,此时position位置为0,limit和capacity为1024
channel.read(byteBuffer),bytebuffer从channel中读取5个数据,这是position=5,limit和capacity为1024
byteBuffer.flip(),为了把byteBuffer的数据写出去,这是limit需要到position的位置,position到初始位置,capacity不变,这样就可以开始把byteBuffer中的数据写出去。position= 0 , limit = 5 , capacity = 1024.
byteBuffer.clear(),回复到原来的状态。position = 0,limit = 1024 , capacity = 1024。
private static void nio1() {
try {
RandomAccessFile file = new RandomAccessFile("./19_io/text.txt", "r");
FileChannel channel = file.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
channel.read(byteBuffer);
byteBuffer.flip();
System.out.println(Charset.defaultCharset().decode(byteBuffer));
byteBuffer.clear();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
网络的非阻塞
serverSocketChannel.configureBlocking(false);
private static void nio2() {
try {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(8080));
serverSocketChannel.configureBlocking(false);
Selector selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
SocketChannel socketChannel = serverSocketChannel.accept();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (socketChannel.read(byteBuffer) != -1) {
byteBuffer.flip();
socketChannel.write(byteBuffer);
byteBuffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Okio
- 它也是基于插管的,是单向的,输⼊源叫 Source,输出⽬标叫 Sink 。
- ⽀持 Buffer
向 NIO ⼀样,可以对 Buffer 进⾏操作
但不强制使⽤ Buffer
okio1()就是作为source作为输入源
okio1()就是使用Okio的buffer作为中转
private static void okio1() {
try (Source source = Okio.buffer(Okio.source(new File("./19_io/text.txt")))) {
Buffer buffer = new Buffer();
source.read(buffer, 1024);
System.out.println(buffer.readUtf8Line());
System.out.println(buffer.readUtf8Line());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void okio2() {
Buffer buffer = new Buffer();
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(buffer.outputStream())) {
objectOutputStream.writeUTF("abc");
objectOutputStream.writeBoolean(true);
objectOutputStream.writeChar('0');
objectOutputStream.flush();//中途使用就要手动flush
ObjectInputStream objectInputStream = new ObjectInputStream(buffer.inputStream());
System.out.println(objectInputStream.readUTF());
System.out.println(objectInputStream.readBoolean());
} catch (IOException e) {
e.printStackTrace();
}
}