CS-Notes/notes/Java IO.md

407 lines
12 KiB
Markdown
Raw Normal View History

2018-02-20 10:40:05 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:22 +08:00
* [????](#????)
* [???????](#???????)
* [??????](#??????)
* [???????](#???????)
* [???????](#???????)
* [???????](#???????)
2018-02-20 10:40:05 +08:00
* [1. InetAddress](#1-inetaddress)
* [2. URL](#2-url)
* [3. Sockets](#3-sockets)
* [4. Datagram](#4-datagram)
* [NIO](#nio)
2018-02-22 14:47:22 +08:00
* [1. ?????](#1-?????)
* [2. ?????????](#2-?????????)
* [2.1 ???](#21-???)
* [2.2 ??????](#22-??????)
* [3. ????????????](#3-????????????)
* [4. ??<3F><>??????](#4-??<3F><>??????)
* [5. ???????????](#5-???????????)
* [5.1 ????? I/O](#51-?????-io)
* [5.2 ??????? I/O](#52-???????-io)
* [6. ????????](#6-????????)
2018-02-20 10:40:05 +08:00
* [6.1 ServerSocketChannel](#61-serversocketchannel)
* [6.2 Selectors](#62-selectors)
2018-02-22 14:47:22 +08:00
* [6.3 ?????](#63-?????)
* [6.4 ??????????](#64-??????????)
* [6.5 ???????????](#65-???????????)
* [6.6 ?????????? SelectionKey](#66-??????????-selectionkey)
* [6.7 ????? I/O](#67-?????-io)
* [?<3F><>?????](#?<3F><>?????)
2018-02-20 10:40:05 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:22 +08:00
# ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
Java ?? I/O ???????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
1. ?????????File
2. ????????InputStream ?? OutputStream
3. ?????????Reader ?? Writer
4. ?????????Serializable
5. ?????????Socket
6. ??????? IO??NIO
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
# ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
File ????????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
# ??????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8143787f-12eb-46ea-9bc3-c66d22d35285.jpg)
2018-02-22 14:47:22 +08:00
Java I/O ???????????????????? InputStream ?????InputStream ??????????FileInputStream ?? InputStream ??????????????????????????????????????FilterInputStream ????????????????????????????????????????????????? BufferedInputStream ? FileInputStream ??????????????????????<3F><>??<3F><>????????????????????? FileInputStream ????????????? BufferedInputStream ????<3F><>?
2018-02-20 10:40:05 +08:00
```java
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
```
2018-02-22 14:47:22 +08:00
DataInputStream ?????????????????????????????????????? int??double ??????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????<3F><>???????????????
2018-02-20 10:40:05 +08:00
```java
byte[] buf = new byte[20*1024];
int bytes = 0;
2018-02-22 14:47:22 +08:00
// ????? buf.length ????????????????????????????? -1 ??????????? eof???????<3F><>
2018-02-20 10:40:05 +08:00
while((bytes = in.read(buf, 0 , buf.length)) != -1) {
// ...
}
```
2018-02-22 14:47:22 +08:00
# ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????<3F><>????<3F><>??<3F><>?????????????????????????? I/O ????????????????????????????????<3F><>??????????????????????????????????????<3F><>??????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
InputStreamReader ?????????????????????????????OutputStreamWriter ??????????????????????????????????????? Reader ?? Writer??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
```java
2018-02-22 14:47:22 +08:00
byte[] bytes = str.getBytes(encoding); // ????
String str = new String(bytes, encoding)?? // ????
2018-02-20 10:40:05 +08:00
```
2018-02-22 14:47:22 +08:00
GBK ?????<3F><>?????? 2 ?????????? 1 ??????UTF-8 ?????<3F><>?????? 3 ?????????? 1 ??????Java ?????????? UTF-16be???????????? 2 ??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????<3F><>??????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
# ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???<3F><>????????????????????????<3F><>?????<3F><>?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???<3F><>???ObjectOutputStream.writeObject()
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????<3F><>???ObjectInputStream.readObject()
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???<3F><>??????????? Serializable ?????????????????????<3F>ʦ<EFBFBD>??????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
transient ??????????<3F><>??????????<3F><>???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**ArrayList ???<3F><>???????<3F><>??????**??ArrayList ?<3F>՛<EFBFBD>????????????? transient ???<3F><>???????????????????????????????<3F><>????????????????????<3F><>???????????<3F><>????????<3F><>???<3F><>???????<3F><>?????????????????<3F><>????????????????????????
2018-02-20 10:40:05 +08:00
```
private transient Object[] elementData;
```
2018-02-22 14:47:22 +08:00
# ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
Java ?<3F><>?????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
1. InetAddress????????????????????????? IP ?????
2. URL?????????<3F><>??????? URL ?????????????<3F><>??????????????
3. Sockets????? TCP <20><>?????????????
4. Datagram????? UDP <20><>?????????????
2018-02-20 10:40:05 +08:00
## 1. InetAddress
2018-02-22 14:47:22 +08:00
??<3F><>??<3F><>??????????????????????????????????? InetAddress.getByName(String host)??InetAddress.getByAddress(byte[] addr)??
2018-02-20 10:40:05 +08:00
## 2. URL
2018-02-22 14:47:22 +08:00
???????? URL ?<3F><>???????????
2018-02-20 10:40:05 +08:00
```java
URL url = new URL("http://www.baidu.com");
2018-02-22 14:47:22 +08:00
InputStream is = url.openStream(); // ?????
InputStreamReader isr = new InputStreamReader(is, "utf-8"); // ?????
2018-02-20 10:40:05 +08:00
BufferedReader br = new BufferedReader(isr);
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
br.close();
isr.close();
is.close();
```
## 3. Sockets
2018-02-22 14:47:22 +08:00
Socket ??????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/fa4101d7-19ce-4a69-a84f-20bbe64320e5.jpg)
2018-02-22 14:47:22 +08:00
- ServerSocket????????????
- Socket?????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????? InputStream ?? OutputStream ?????????????
2018-02-20 10:40:05 +08:00
## 4. Datagram
2018-02-22 14:47:22 +08:00
- DatagramPacket?????????
- DatagramSocket???????
2018-02-20 10:40:05 +08:00
# NIO
2018-02-22 14:47:22 +08:00
NIO ???????? I/O ???? ( ??????????????? ) ?????????????? ??????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## 1. ?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
I/O ?? NIO ?????????????????????????????????????????I/O ??????????????????? NIO ???????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????? I/O ???????????<3F><>????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? I/O ?????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????? I/O ??????????????????????????????????<3F><>??????????????????<3F><>???<3F><>????????????????????????????????? I/O ????<3F><>???????? I/O ?????<3F><>????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
I/O ???? NIO ?????????????java.io.\* ????? NIO ????????????????????????????????? NIO ???<3F><>????????<3F><> java.io.\* ???<3F><>??<3F><>??????????????<3F><>??????????????<3F><>????????????????<3F><>??????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## 2. ?????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 2.1 ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??? Channel ???? I/O ???<3F><>??????????????????????<3F><>???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????????????????????(??????????? InputStream ???? OutputStream ??????)?? ???????????????????????<3F><>???????????<3F><>??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
- FileChannel????????<3F><>?<3F><>?????
- DatagramChannel????? UDP ??<3F><>???????????
- SocketChannel????? TCP ??<3F><>???????????
- ServerSocketChannel???????????????? TCP ?????????????????????????????? SocketChannel??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 2.2 ??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????<3F><>???????????????????<3F><>?????????????<3F><>?????<3F><>??????????????????<3F><>???????????????????????<3F><>?<3F><>??????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????<3F><>??????????????????<3F><>?????????????????????????????????????????/<2F><>?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????
2018-02-20 10:40:05 +08:00
- ByteBuffer
- CharBuffer
- ShortBuffer
- IntBuffer
- LongBuffer
- FloatBuffer
- DoubleBuffer
2018-02-22 14:47:22 +08:00
## 3. ????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
- capacity???????????
- position??????????<3F><>?????????
- limit?????????<3F><>?????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
1\. ????????<3F><>? 8 ???????????????? position ? 0???? limit == capacity == 9??capacity ???????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1bea398f-17a7-4f67-a90b-9e2d243eaa9a.png)
2018-02-22 14:47:22 +08:00
2\. ??????????<3F><>?? 3 ?????????<3F><>???????<3F><>???? position ?????? 3??limit ???????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4628274c-25b6-4053-97cf-d1239b44c43d.png)
2018-02-22 14:47:22 +08:00
3\. ???????????????<3F><>???????????????????? flip() ??????????????? limit ???????? position?????? position ????? 0??
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/952e06bd-5a65-4cab-82e4-dd1536462f38.png)
2018-02-22 14:47:22 +08:00
4\. ?????????? 4 ??????????????<3F><>???? position ??? 4??
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b5bdcbe2-b958-4aef-9151-6ad963cb28b4.png)
2018-02-22 14:47:22 +08:00
5\. ?????????? clear() ??????????????????? position ?? limit ????????????<3F><>?<3F><>?
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/67bf5487-c45d-49b6-b9c0-a058d8c68902.png)
2018-02-22 14:47:22 +08:00
## 4. ??<3F><>??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
1\. ?????????????? FileInputStream???????? FileInputStream ??????? FileChannel??
2018-02-20 10:40:05 +08:00
```java
FileInputStream fin = new FileInputStream("readandshow.txt");
FileChannel fic = fin.getChannel();
```
2018-02-22 14:47:22 +08:00
2\. ???????????? 1024 ?? Buffer
2018-02-20 10:40:05 +08:00
```java
ByteBuffer buffer = ByteBuffer.allocate(1024);
```
2018-02-22 14:47:22 +08:00
3\. ??????????? FileChannel <20><>?? Buffer ?<3F><>??????????????? read() ???????? -1
2018-02-20 10:40:05 +08:00
```java
int r = fcin.read(buffer);
if (r == -1) {
break;
}
```
2018-02-22 14:47:22 +08:00
4\. ??<3F><>?????????? FileOutputStream???????? FileOutputStream ?????? FileChannel
2018-02-20 10:40:05 +08:00
```java
FileOutputStream fout = new FileOutputStream("writesomebytes.txt");
FileChannel foc = fout.getChannel();
```
2018-02-22 14:47:22 +08:00
5\. ???? flip() ?<3F><>???<3F><>
2018-02-20 10:40:05 +08:00
```java
buffer.flip();
```
2018-02-22 14:47:22 +08:00
6\. ?? Buffer ?<3F><>???????????? FileChannel ??
2018-02-20 10:40:05 +08:00
```java
foc.write(buffer);
```
2018-02-22 14:47:22 +08:00
7\. ?????? clear() ?????????
2018-02-20 10:40:05 +08:00
```java
buffer.clear();
```
2018-02-22 14:47:22 +08:00
## 5. ???????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????FileChannel ?????<3F><>?????????????????? Channel ?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 5.1 ????? I/O
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????? I/O ????? InputStream.read() ?????????????????????????????????????? ServerSocket.accept() ???????????????????<3F><>??????????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/edc23f99-c46c-4200-b64e-07516828720d.jpg)
2018-02-22 14:47:22 +08:00
### 5.2 ??????? I/O
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????<3F><>? I/O ???????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????? wait()??notify() ?????????????????????<3F><>???????????????????<3F><>??????<3F><>???
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7fcb2fb0-2cd9-4396-bc2d-282becf963c3.jpg)
2018-02-22 14:47:22 +08:00
## 6. ????????
2018-02-20 10:40:05 +08:00
### 6.1 ServerSocketChannel
2018-02-22 14:47:22 +08:00
???????????????? ServerSocketChannel ?????????????
2018-02-20 10:40:05 +08:00
```java
ServerSocketChannel ssc = ServerSocketChannel.open();
2018-02-22 14:47:22 +08:00
ssc.configureBlocking(false); // ???????????
2018-02-20 10:40:05 +08:00
ServerSocket ss = ssc.socket();
InetSocketAddress address = new InetSocketAddress(ports[i]);
2018-02-22 14:47:22 +08:00
ss.bind(address); // ?????
2018-02-20 10:40:05 +08:00
```
### 6.2 Selectors
2018-02-22 14:47:22 +08:00
?? I/O ??? Selector ??????? I/O ???????? ?D ?????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???? Selectors ?????????????????????? register() ??????register() ????????????????? Selector????????????? OP_ACCEPT?????????????????????? accept ????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
SelectionKey ????????????? Selector ?????????????? Selector ????????????????????????????????????? SelectionKey ?????<3F><>??SelectionKey ?????????????????????
2018-02-20 10:40:05 +08:00
```java
Selector selector = Selector.open();
SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);
```
2018-02-22 14:47:22 +08:00
### 6.3 ?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????? Selector ?? select() ??????????????????????????????????????????????????????????????????????? select() ?????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????? Selector ?? selectedKeys() ?????????????????????? SelectionKey ???????? ???? ??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????? SelectionKeys ?????<3F><>?????? SelectionKey ??????????????????? SelectionKey????????????????????? I/O ???????????????????<3F><> I/O ????
2018-02-20 10:40:05 +08:00
```java
int num = selector.select();
Set selectedKeys = selector.selectedKeys();
Iterator it = selectedKeys.iterator();
while (it.hasNext()) {
SelectionKey key = (SelectionKey)it.next();
// ... deal with I/O event ...
}
```
2018-02-22 14:47:22 +08:00
### 6.4 ??????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????<3F><>??????????????? ServerSocketChannel??????????????????????????????????????? SelectionKey ???? readyOps() ??????????<3F><>????????????????
2018-02-20 10:40:05 +08:00
```java
if ((key.readyOps() & SelectionKey.OP_ACCEPT)
== SelectionKey.OP_ACCEPT) {
// Accept the new connection
// ...
}
```
2018-02-22 14:47:22 +08:00
??????????? readOps() ??????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 6.5 ???????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????????????????????????????????????????????????????? accept() ????????????
2018-02-20 10:40:05 +08:00
```java
ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
SocketChannel sc = ssc.accept();
```
2018-02-22 14:47:22 +08:00
??????????????? SocketChannel ??????????????????????????????????????????????????????????????????????? SocketChannel ??? Selector????????????
2018-02-20 10:40:05 +08:00
```java
sc.configureBlocking( false );
SelectionKey newKey = sc.register( selector, SelectionKey.OP_READ );
```
2018-02-22 14:47:22 +08:00
?????????? register() ?? OP_READ ???????? SocketChannel ??????? ??? ?????? ???? ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 6.6 ?????????? SelectionKey
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????? SelectionKey ???????????????????????????????????????????????? SelectionKey ????????????????????????????????????????????????????????????????????????????????????????????<3F><>????????????????????? remove() ???????????????? SelectionKey??
2018-02-20 10:40:05 +08:00
```java
it.remove();
```
2018-02-22 14:47:22 +08:00
???????????????????????????????????<3F><>????????(???????????? I/O ???)???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 6.7 ????? I/O
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????????? I/O ??????????????????<3F><>??? Selector.select()????????????????? I/O ?????????<3F><>? SelectionKey ???????? OP_READ ??????????????
2018-02-20 10:40:05 +08:00
```java
} else if ((key.readyOps() & SelectionKey.OP_READ)
== SelectionKey.OP_READ) {
// Read the data
SocketChannel sc = (SocketChannel)key.channel();
// ...
}
```
2018-02-22 14:47:22 +08:00
# ?<3F><>?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
- Eckel B, ????? , ??? , ?? . Java ?????? [M]. ??<3F><>????????? , 2002.
- [IBM: NIO ????](https://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html)
- [ ??????? Java I/O ????????? ](https://www.ibm.com/developerworks/cn/java/j-lo-javaio/index.html)
- [NIO ??? IO ?????? ](http://blog.csdn.net/shimiso/article/details/24990499)