Java I/O ?????????????
???????????? ???????[ 2015/8/10 10:36:53 ] ??????????????? ???????????
????Java I/O
????I/O???? Input/Output(????/???) ????? I/O ????????????? 5 ??????blocking I/O??nonblocking I/O??I/O multiplexing (select and poll)??signal driven I/O (SIGIO)??asynchronous I/O (the POSIX aio_functions)????????????????????????????UNIX ??? IO ??·???á???????з??????freebsd ????? kqueue??Linux ?? epoll???? Windows2000 ????????? IOCP ??????? asynchronous I/O??
????Java ????????????????????? I/O???????? NIO??Java1.4 ????? NIO1.0 ????? I/O ??????????????????????????÷????Linux ??? epoll??BSD ???? kqueue??Windows ??????? I/O??
????Java I/O ??????????????????
????????????? (I/O ????)????????????????????????????????????????????? I/O ????????? I/O ???????????????????????????????????????????????д??????????????????????κ?????????????????????????????????????????????????I/O ???????????????????????????????е????????????????????? Socket ???? I/O ????????????????????????????????????????????? Socket ???????????TCP ???????????????????????????????????????????????????? CPU ??????????????????????????????????? Socket ??????????????????????? Socket ???е?????? (???????????????)???????????к???????????????????????????????????????????????Ч?????????????????????????????????????????????????????????????????????????????????
????????????? (NIO ????)???????????????????????????????????????????? I/O??????????????????????? I/O ????????????? I/O ??????????????????????????????·???????????·????????????????? I/O ??????????????????д????NIO ??????????????????????????????????????????????????????????????????????????????? (??????????????????) ?????????????????????????? Socket ?????б??????????б??????????????????? Socket ?????????????????????? Socket ?????????????????????????? Socket ????????????д????????? Socket ????????д???????????????? Socket ????????ж??????????????????????????????????????÷??????????Ч?????????????
???????????? (AIO ??????JDK7 ????)???????????????????Ч???????????????????? I/O ??????????????????????????????????????????д???????????????????? I/O?????????????????????????????????? I/O ?????????????????????????????????????? Linux ?? epoll ????
?????????????????????£???? I/O ????д??????????????????????????????????????????????? I/O ?????????????????????????????????Ч?????????????????????????????????????????????????????????????????????????????????????????? I/O ??????????????????????????????? I/O ????????????????????????????? IO ???????????????????????????????????????? 10000 ????????????? I/O ???????? 10000 ????????????? 1000 ??????????? 2000 ????????????????????? IO ???????????????????????????????????????????????????????????????????“???”?????????“???”???????????????????????????????????????????????У??????????黹???????????????????????????????????? I/O ??????????????????????????????
????Java NIO
????Java.nio ???? Java ?? 1.4 ?汾??????????????????????? I/O ??????Ч???
?????? 1 ????? I/O ?? NIO ???????????
?????? 1. I/O VS NIO
????I/ONIO
????????????????
???????? IO?????? IO
???????????
????NIO ?????? (Block) ???????????????λ??????????? NIO ?У??????????????????? Buffer ????? Channel????????????????????飬?? NIO ??д?????????????????????????????????????????????????????д???????????????????Channel ???????????????????????д??Stream ?????????ó????????? Channel ???ж?д??????????????? Buffer ?????У??? Channel ????? Buffer ????д??????
??????? Buffer ??д????????????????????裺
????д??????? Buffer??
???????? flip() ??????
?????? Buffer ?ж???????
???????? clear() ???????? compact() ??????
???????? Buffer д?????????Buffer ??????д???????????????????????????? flip() ?????? Buffer ??д???л???????????????£?????????д?? Buffer ???????????
????????????????е?????????????????????????????α?д?????????????????????????? clear() ?? compact() ??????clear() ?????????????????????compact() ?????????????????????????κ?δ???????????????????????????????д?????????????????δ?????????檔
????Buffer ?ж????????????? Buffer ????????????? Buffer ?е??????
????? 1 Buffer ??????
????Buffer д???????????????
?????? Channel д?? Buffer?????????? Channel ??????ж???????д?? Channel??
?????????? put ????????????д?????
?????? Buffer ?ж????????????????
?????? Buffer ???????? Channel??
??????? get() ?????? Buffer ?ж???????
????Buffer ?? rewin ?????? position ??? 0?????????????? Buffer ?е??????????limit ???????????????? Buffer ?ж???????????byte??char ?????
????clear() ?? compact() ????
??????????? Buffer ?е??????????? Buffer ???????α?д????????? clear() ?? compact() ????????ɡ?
?????????????? clear() ??????position ??????? 0??limit ?????ó? capacity ????????仰???Buffer ????????Buffer ?е??????δ??????????Щ????????????????????? Buffer ??д?????
??????? Buffer ?????Щδ????????????? clear() ???????????“??????”????ζ????????κα??????????Щ?????????????Щ????С???? Buffer ??????δ?????????????????????Щ????????????????дЩ??????????? compact() ??????compact() ??????????δ????????????? Buffer ?????????? position ?赽?????δ??????????檔limit ????????? clear() ????????????ó? capacity?????? Buffer ?????д????????????????δ?????????
????Buffer ????
????Buffer ?? 3 ????????????λ?? (position)?????? (capacity) ?????? (limit)??
????capacity ??? Buffer ???С???? Buffer ?????????????????
????limit ?? Buffer ????д???????????д???????????????????????ж?????????????
????position ?? Buffer ????д??????????д?????λ????????????????????????????λ?á????д????????position+1????? limit ?? position ?? Buffer ???/д?????岻??????????? Buffer ?? flip ????????д????????????limit(??)=position(д)??position(??) =0??
???????&???
????NIO ??????????????????????????? (Scattering) ???? (Gathering)???????????????????? Buffer ?У?????????????????????????????????д????? Buffer ?С??????????????÷????????? Buffer ?????????÷??????????????????У??????????????????????????????????????????????????????????????????????????????????????????????????????????£??????????????????????? Buffer???????? Buffer ???С??÷?????????ν????С????????????????????????ν????????????????? Buffer ?У????????????????????????????????????????????С????? Buffer ??????t??д?????????????????????????嵥 1 ?? FileChannel ?????????????????????д?????????
?????嵥 1. ???????????д???????
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NIOScatteringandGathering {
public void createFiles(String TPATH){
try {
ByteBuffer bookBuf = ByteBuffer.wrap("java ???????????".getBytes("utf-8"));
ByteBuffer autBuf = ByteBuffer.wrap("test".getBytes("utf-8"));
int booklen = bookBuf.limit();
int autlen = autBuf.limit();
ByteBuffer[] bufs = new ByteBuffer[]{bookBuf??autBuf};
File file = new File(TPATH);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(file);
FileChannel fc = fos.getChannel();
fc.write(bufs);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByteBuffer b1 = ByteBuffer.allocate(booklen);
ByteBuffer b2 = ByteBuffer.allocate(autlen);
ByteBuffer[] bufs1 = new ByteBuffer[]{b1??b2};
File file1 = new File(TPATH);
try {
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
fc.read(bufs1);
String bookname = new String(bufs1[0].array()??"utf-8");
String autname = new String(bufs1[1].array()??"utf-8");
System.out.println(bookname+" "+autname);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
NIOScatteringandGathering nio = new NIOScatteringandGathering();
nio.createFiles("C:\1.TXT");
}
}
????????????嵥 2 ?????
?????嵥 2. ???н??
????java ??????????? test
??????

???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11