407 lines
12 KiB
Markdown
407 lines
12 KiB
Markdown
<!-- GFM-TOC -->
|
|
* [????](#????)
|
|
* [???????](#???????)
|
|
* [??????](#??????)
|
|
* [???????](#???????)
|
|
* [???????](#???????)
|
|
* [???????](#???????)
|
|
* [1. InetAddress](#1-inetaddress)
|
|
* [2. URL](#2-url)
|
|
* [3. Sockets](#3-sockets)
|
|
* [4. Datagram](#4-datagram)
|
|
* [NIO](#nio)
|
|
* [1. ?????](#1-?????)
|
|
* [2. ?????????](#2-?????????)
|
|
* [2.1 ???](#21-???)
|
|
* [2.2 ??????](#22-??????)
|
|
* [3. ????????????](#3-????????????)
|
|
* [4. ??§Õ??????](#4-??§Õ??????)
|
|
* [5. ???????????](#5-???????????)
|
|
* [5.1 ????? I/O](#51-?????-io)
|
|
* [5.2 ??????? I/O](#52-???????-io)
|
|
* [6. ????????](#6-????????)
|
|
* [6.1 ServerSocketChannel](#61-serversocketchannel)
|
|
* [6.2 Selectors](#62-selectors)
|
|
* [6.3 ?????](#63-?????)
|
|
* [6.4 ??????????](#64-??????????)
|
|
* [6.5 ???????????](#65-???????????)
|
|
* [6.6 ?????????? SelectionKey](#66-??????????-selectionkey)
|
|
* [6.7 ????? I/O](#67-?????-io)
|
|
* [?¦Ï?????](#?¦Ï?????)
|
|
<!-- GFM-TOC -->
|
|
|
|
# ????
|
|
|
|
Java ?? I/O ???????????????
|
|
|
|
1. ?????????File
|
|
2. ????????InputStream ?? OutputStream
|
|
3. ?????????Reader ?? Writer
|
|
4. ?????????Serializable
|
|
5. ?????????Socket
|
|
6. ??????? IO??NIO
|
|
|
|
# ???????
|
|
|
|
File ????????????????????????????????????????????????????????????
|
|
|
|
# ??????
|
|
|
|

|
|
|
|
Java I/O ???????????????????? InputStream ?????InputStream ??????????FileInputStream ?? InputStream ??????????????????????????????????????FilterInputStream ????????????????????????????????????????????????? BufferedInputStream ? FileInputStream ??????????????????????§Ý??œ¤????????????????????? FileInputStream ????????????? BufferedInputStream ????¨À?
|
|
|
|
```java
|
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
|
```
|
|
|
|
DataInputStream ?????????????????????????????????????? int??double ??????????
|
|
|
|
????????????§Ö???????????????
|
|
|
|
```java
|
|
byte[] buf = new byte[20*1024];
|
|
int bytes = 0;
|
|
// ????? buf.length ????????????????????????????? -1 ??????????? eof???????¦Â
|
|
while((bytes = in.read(buf, 0 , buf.length)) != -1) {
|
|
// ...
|
|
}
|
|
```
|
|
|
|
# ???????
|
|
|
|
???????????????×Ï????§³??›¥?????????????????????????? I/O ????????????????????????????????§Ó??????????????????????????????????????§Ó??????????
|
|
|
|
InputStreamReader ?????????????????????????????OutputStreamWriter ??????????????????????????????????????? Reader ?? Writer??
|
|
|
|
????????????????????????????????????????????
|
|
|
|
```java
|
|
byte[] bytes = str.getBytes(encoding); // ????
|
|
String str = new String(bytes, encoding)?? // ????
|
|
```
|
|
|
|
GBK ?????§µ?????? 2 ?????????? 1 ??????UTF-8 ?????§µ?????? 3 ?????????? 1 ??????Java ?????????? UTF-16be???????????? 2 ??????
|
|
|
|
????????????????¨°??????????????????????
|
|
|
|
# ???????
|
|
|
|
???§Ý????????????????????????§µ?????›¥?????
|
|
|
|
???§Ý???ObjectOutputStream.writeObject()
|
|
|
|
?????§Ý???ObjectInputStream.readObject()
|
|
|
|
???§Ý??????????? Serializable ?????????????????????¦Ê¦Ç??????????
|
|
|
|
transient ??????????§»??????????§Ý???
|
|
|
|
**ArrayList ???§Ý???????§Ý??????**??ArrayList ?§Õ›¥????????????? transient ???¦Å???????????????????????????????§Ö????????????????????§Ö???????????§Ý????????§Õ???§Ý???????§Ý?????????????????§Ý????????????????????????
|
|
|
|
```
|
|
private transient Object[] elementData;
|
|
```
|
|
|
|
# ???????
|
|
|
|
Java ?§Ö?????????
|
|
|
|
1. InetAddress????????????????????????? IP ?????
|
|
2. URL?????????¦Ë??????? URL ?????????????§Õ??????????????
|
|
3. Sockets????? TCP §¿?????????????
|
|
4. Datagram????? UDP §¿?????????????
|
|
|
|
## 1. InetAddress
|
|
|
|
??§Û??§Û??????????????????????????????????? InetAddress.getByName(String host)??InetAddress.getByAddress(byte[] addr)??
|
|
|
|
## 2. URL
|
|
|
|
???????? URL ?§Ø???????????
|
|
|
|
```java
|
|
URL url = new URL("http://www.baidu.com");
|
|
InputStream is = url.openStream(); // ?????
|
|
InputStreamReader isr = new InputStreamReader(is, "utf-8"); // ?????
|
|
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
|
|
|
|
Socket ??????
|
|
|
|

|
|
|
|
- ServerSocket????????????
|
|
- Socket?????????
|
|
|
|
??????????????? InputStream ?? OutputStream ?????????????
|
|
|
|
## 4. Datagram
|
|
|
|
- DatagramPacket?????????
|
|
- DatagramSocket???????
|
|
|
|
# NIO
|
|
|
|
NIO ???????? I/O ???? ( ??????????????? ) ?????????????? ??????????????????????????????????
|
|
|
|
## 1. ?????
|
|
|
|
I/O ?? NIO ?????????????????????????????????????????I/O ??????????????????? NIO ???????????????
|
|
|
|
???????? I/O ???????????§Õ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? I/O ?????????
|
|
|
|
????????? I/O ??????????????????????????????????§Ó??????????????????î•???ø„????????????????????????????????? I/O ????§»???????? I/O ?????§Ö????????????
|
|
|
|
I/O ???? NIO ?????????????java.io.\* ????? NIO ????????????????????????????????? NIO ???§»????????íà java.io.\* ???§Ö??§»??????????????§Õ??????????????¨¹????????????????§µ??????????????
|
|
|
|
## 2. ?????????
|
|
|
|
### 2.1 ???
|
|
|
|
??? Channel ???? I/O ???§Ö??????????????????????§Õ???????
|
|
|
|
????????????????????????????????????????(??????????? InputStream ???? OutputStream ??????)?? ???????????????????????§Õ???????????§Õ??
|
|
|
|
????????????????
|
|
|
|
- FileChannel????????§Ø?§Õ?????
|
|
- DatagramChannel????? UDP ??§Õ???????????
|
|
- SocketChannel????? TCP ??§Õ???????????
|
|
- ServerSocketChannel???????????????? TCP ?????????????????????????????? SocketChannel??
|
|
|
|
### 2.2 ??????
|
|
|
|
????????????????§Ø???????????????????§µ?????????????§Ø?????¦Ê??????????????????§³???????????????????????§Ø?§Õ??????????????????????
|
|
|
|
???????????????????ï…??????????????????î•?????????????????????????????????????????/§Õ?????
|
|
|
|
???????????????????
|
|
|
|
- ByteBuffer
|
|
- CharBuffer
|
|
- ShortBuffer
|
|
- IntBuffer
|
|
- LongBuffer
|
|
- FloatBuffer
|
|
- DoubleBuffer
|
|
|
|
|
|
## 3. ????????????
|
|
|
|
- capacity???????????
|
|
- position??????????§Õ?????????
|
|
- limit?????????§Õ?????????
|
|
|
|
??????????????
|
|
|
|
1\. ????????§³? 8 ???????????????? position ? 0???? limit == capacity == 9??capacity ???????????????????????????
|
|
|
|

|
|
|
|
2\. ??????????§Ø?? 3 ?????????§Õ???????§µ???? position ?????? 3??limit ???????
|
|
|
|

|
|
|
|
3\. ???????????????§Õ???????????????????? flip() ??????????????? limit ???????? position?????? position ????? 0??
|
|
|
|

|
|
|
|
4\. ?????????? 4 ??????????????§µ???? position ??? 4??
|
|
|
|

|
|
|
|
5\. ?????????? clear() ??????????????????? position ?? limit ????????????¦Ë?¨¢?
|
|
|
|

|
|
|
|
## 4. ??§Õ??????
|
|
|
|
1\. ?????????????? FileInputStream???????? FileInputStream ??????? FileChannel??
|
|
|
|
```java
|
|
FileInputStream fin = new FileInputStream("readandshow.txt");
|
|
FileChannel fic = fin.getChannel();
|
|
```
|
|
|
|
2\. ???????????? 1024 ?? Buffer
|
|
|
|
```java
|
|
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
|
```
|
|
|
|
3\. ??????????? FileChannel §Õ?? Buffer ?§µ??????????????? read() ???????? -1
|
|
|
|
```java
|
|
int r = fcin.read(buffer);
|
|
if (r == -1) {
|
|
break;
|
|
}
|
|
```
|
|
|
|
4\. ??§Õ?????????? FileOutputStream???????? FileOutputStream ?????? FileChannel
|
|
|
|
```java
|
|
FileOutputStream fout = new FileOutputStream("writesomebytes.txt");
|
|
FileChannel foc = fout.getChannel();
|
|
```
|
|
|
|
5\. ???? flip() ?§Ý???§Õ
|
|
|
|
```java
|
|
buffer.flip();
|
|
```
|
|
|
|
6\. ?? Buffer ?§Ö???????????? FileChannel ??
|
|
|
|
```java
|
|
foc.write(buffer);
|
|
```
|
|
|
|
7\. ?????? clear() ?????????
|
|
|
|
```java
|
|
buffer.clear();
|
|
```
|
|
|
|
## 5. ???????????
|
|
|
|
??????FileChannel ?????§Ý?????????????????? Channel ?????
|
|
|
|
### 5.1 ????? I/O
|
|
|
|
????? I/O ????? InputStream.read() ?????????????????????????????????????? ServerSocket.accept() ???????????????????§á??????????????????????????????????????????????????????????????
|
|
|
|

|
|
|
|
### 5.2 ??????? I/O
|
|
|
|
?????????????????????§Ö? I/O ???????????????
|
|
|
|
??????????????????????????????????????????????
|
|
|
|
???????????????? wait()??notify() ?????????????????????§Ý???????????????????¦Í??????§Ý???
|
|
|
|

|
|
|
|
## 6. ????????
|
|
|
|
### 6.1 ServerSocketChannel
|
|
|
|
???????????????? ServerSocketChannel ?????????????
|
|
|
|
```java
|
|
ServerSocketChannel ssc = ServerSocketChannel.open();
|
|
ssc.configureBlocking(false); // ???????????
|
|
|
|
ServerSocket ss = ssc.socket();
|
|
InetSocketAddress address = new InetSocketAddress(ports[i]);
|
|
ss.bind(address); // ?????
|
|
```
|
|
|
|
### 6.2 Selectors
|
|
|
|
?? I/O ??? Selector ??????? I/O ???????? ?D ?????????????????????????????????????????????????????????
|
|
|
|
???? Selectors ?????????????????????? register() ??????register() ????????????????? Selector????????????? OP_ACCEPT?????????????????????? accept ????????????????????????????????????
|
|
|
|
SelectionKey ????????????? Selector ?????????????? Selector ????????????????????????????????????? SelectionKey ?????§Ö??SelectionKey ?????????????????????
|
|
|
|
```java
|
|
Selector selector = Selector.open();
|
|
SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);
|
|
```
|
|
|
|
### 6.3 ?????
|
|
|
|
???????????? Selector ?? select() ??????????????????????????????????????????????????????????????????????? select() ?????????????????????????????
|
|
|
|
??????????????? Selector ?? selectedKeys() ?????????????????????? SelectionKey ???????? ???? ??
|
|
|
|
??????????? SelectionKeys ?????¦Ä?????? SelectionKey ??????????????????? SelectionKey????????????????????? I/O ???????????????????§» I/O ????
|
|
|
|
```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 ...
|
|
}
|
|
```
|
|
|
|
### 6.4 ??????????
|
|
|
|
??????§Ö??????????????? ServerSocketChannel??????????????????????????????????????? SelectionKey ???? readyOps() ??????????ú€????????????????
|
|
|
|
```java
|
|
if ((key.readyOps() & SelectionKey.OP_ACCEPT)
|
|
== SelectionKey.OP_ACCEPT) {
|
|
// Accept the new connection
|
|
// ...
|
|
}
|
|
```
|
|
|
|
??????????? readOps() ??????????????????????????
|
|
|
|
### 6.5 ???????????
|
|
|
|
?????????????????????????????????????????????????????????????????????????????? accept() ????????????
|
|
|
|
```java
|
|
ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
|
|
SocketChannel sc = ssc.accept();
|
|
```
|
|
|
|
??????????????? SocketChannel ??????????????????????????????????????????????????????????????????????? SocketChannel ??? Selector????????????
|
|
|
|
```java
|
|
sc.configureBlocking( false );
|
|
SelectionKey newKey = sc.register( selector, SelectionKey.OP_READ );
|
|
```
|
|
|
|
?????????? register() ?? OP_READ ???????? SocketChannel ??????? ??? ?????? ???? ???????
|
|
|
|
### 6.6 ?????????? SelectionKey
|
|
|
|
????? SelectionKey ???????????????????????????????????????????????? SelectionKey ????????????????????????????????????????????????????????????????????????????????????????????¦Ä????????????????????? remove() ???????????????? SelectionKey??
|
|
|
|
```java
|
|
it.remove();
|
|
```
|
|
|
|
???????????????????????????????????§Õ????????(???????????? I/O ???)???
|
|
|
|
### 6.7 ????? I/O
|
|
|
|
?????????????????????????????????? I/O ??????????????????§Ö??? Selector.select()????????????????? I/O ?????????¦²? SelectionKey ???????? OP_READ ??????????????
|
|
|
|
```java
|
|
} else if ((key.readyOps() & SelectionKey.OP_READ)
|
|
== SelectionKey.OP_READ) {
|
|
// Read the data
|
|
SocketChannel sc = (SocketChannel)key.channel();
|
|
// ...
|
|
}
|
|
```
|
|
|
|
|
|
# ?¦Ï?????
|
|
|
|
- Eckel B, ????? , ??? , ?? . Java ?????? [M]. ??§Ö????????? , 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)
|