1.java.ioä¸å æ¬è¾å ¥åè¾åº2ä¸IOæµï¼æ¯ç§IOæµåå为åèæµåå符æµä¸¤å¤§ç±»ï¼æç §æ¯å¦ç´æ¥ä¸ç®çæºäº¤äºåå为èç¹æµåå¤çæµï¼å¤çæµå ¶å®æ¯å°è£ äºèç¹æµå¨æ´é«å±å®ç°æ°æ®çå¤çï¼è¿æ ·javaç¨åºå¯ä»¥ä½¿ç¨ä¸è´ç代ç ç±å¤çä¸åçèç¹æµï¼ä¸ç®¡æ¥ä¹æ件è¿æ¯ç½ç»è¿æ¯é®çï¼è¯¦è§ä¸å¾ï¼ãjava7æä¾äºNIOæ´é«æãjavaç¨åºå¯ä»¥æ对象åºååæåèæµæ¾å¨ç£çæè ç½ä¸ä¼ éï¼è¿æ¯åå¸å¼ç¼ç¨çåºç¡ã
2.ä¸ææ¯1个å符å ç¨2个åèï¼è±ææ¯1个å符å ç¨1个åèï¼å ¶ä»æ åµè¿è¦æ ¹æ®ç¼ç æ ¼å¼ãjava使ç¨Unicodeæ以ä¸ä¸ªå符å ç¨2个åèãå¨å¤çæµçæ¶åå¦æéè¦å¯¹æµä¸çå 容è¿è¡éè¡å¤çæ¶éè¦ç¨å符æµï¼å¦æä¸éè¦å¤ç使ç¨åèæµãjavaä¸ä¸è¬ä»¥streamç»å°¾é½æ¯åèæµï¼writeråreaderç»å°¾çæ¯å符æµã
3.java.ioä¸æä¾File类访é®æ件åæ件夹ï¼å®ä¸å¹³å°æ å ³ï¼å¯¹æ件è¿è¡å¢å éå½åçï¼ä½æ¯å¯¹æ件å 容ä¸è½è®¿é®ï¼æ件å 容éè¦ä½¿ç¨è¾å ¥è¾åºæµãæä¾æ件åæ¹æ³ï¼å¦ï¼getName()ï¼ãæ件æ£æ¥æ¹æ³(å¦ï¼exists())ãè·åæ件常è§ä¿¡æ¯(å¦ï¼length())ãæ件æä½ï¼å¦ï¼delete()ï¼ãç®å½æä½(mkdir(),listFiles())ãå¨file使ç¨listæ¹æ³ååºæ件夹ä¸çå ¶ä»æ件æ¶æä¾äºä¸ä¸ªæ¹æ³æ¥å£ï¼FilenameFilterï¼ï¼å¯ä»¥ä½¿ç¨lambda表达å¼ã
4.javaè¾å ¥æµé½æ¯ä»¥inputstreamåreaderä½ä¸ºåºç±»ï¼é½ç¨readæ¹æ³æä¾æ°æ®è¯»åï¼ï¼è¾åºæµé½æ¯ä»¥outputstreamåwriterä½ä¸ºåºç±»ï¼é½ç¨ï¼ï¼ä»ä»¬é½æ¯æ½è±¡åºç±»ã以ä¸æ¯IO模ååæµçç±»å ³ç³»
5.inputstreamæä¾ï¼FileInputStreamå®ç°æä½æ件ï¼åreaderï¼FileReaderå®ç°ç±»æä½æ件ï¼ï¼ä»¥ä¸æ¯ä¸ä¸ªä¾åï¼
    FileInputStream d;
      byte[] ds=new byte[100];
      int flage;
      if((flage= d.read(ds))>0)
      {
        System.out.println(new String(ds, 0, flage));
      }
    d.close();//å¨java7以åéåäºIOï¼åªéè¦æ代ç æ¾å¨tryåä¸å°±å¯ä»¥å®ç°èªå¨å ³éæ件ã
è¿ç§æ¹æ³åºè¯¥ç¨å符æµå¤çï¼å 为åèæµå¯è½å读å°æåä¸åçåèï¼å¨è½¬æ¢ææåæ¶å¯è½åºç°ä¹±ç æ åµãï¼æåæµå¤ç类似ï¼
      FileInputStream i=new FileInputStream("1.txt");
      FileOutputStream o=new FileOutputStream("2.txt");
      byte[] buff=new byte[32];
      int hasread=0;
      while((hasread=i.read(buff))>0)
      {
         o.write(buff, 0, hasread);
      }
ç»åè¾å ¥å®ç°
      FileWriter fw=new FileWriter("1.txt");
      fw.write("1111");
      fw.write("2222");
ç®æçç´æ¥åå ¥æ件ï¼ä½¿ç¨FileWriter
6.å¨æä½æµçæ¶åä¸è¬æ¯å»ºç«èç¹æµï¼ç¶ååç¨å¯¹åºçç¼å²æµå è£ è¯¥èç¹æµå®ç°æä½åæ§è½æ´ä¼ã
      //æsystem.in转æ¢æå符æµ
      InputStreamReader reader=new InputStreamReader(System.in);
      //æèç¹å符æµè½¬æ¢æ¢æç¼å²æµ
      BufferedReader br=new BufferedReader(reader);
      String line=null;
      while((line=br.readLine())!=null)
      {
         System.out.println(line);
      }
7.æ件çææérandomAccessFile,å®æä¾äºå¯¹æ件ï¼å ¶å®IOç¹ä¸è¡ï¼çéæºè¯»åï¼æ¯å¦è¯»åæä¸æ®µæè 追å æ件å 容ãæ¤ç±»ææ件记å½æéçæ¦å¿µï¼å¯¹æéæè·åå设置2个æ¹æ³ãå®ç读类似inputstreamç3个readæ¹æ³ï¼å类似outputstreamçwriteæ¹æ³ãè¿æä¸äºåçwriteXXåreadXXæ¹æ³ãå¦ææ³å¨æ件æ个ä½ç½®æå ¥å è¦ææ件æå ¥ä½ç½®åçæ件ä¿åèµ·æ¥å¨å å ¥æå ¥å 容ååå ä¼ä¹åçåé¢å 容ï¼ä¸ç¶ä¼åºç°è¦çã
      try(RandomAccessFile raf=new RandomAccessFile("1.txt", "rw"))
      {
       //å¼å§è¯»æ件
         raf.seek(300);
         byte[] bbuf=new byte[1024];
         int hasread=0;
         while((hasread=raf.read(bbuf))>0)
         {
            System.out.println(new String(bbuf, 0, hasread));
         }
         //å¼å§è¿½å
         raf.seek(raf.length());
         raf.write("追å å 容".getBytes());
         //
    }
    catch(IOException ex)
    {
       ex.printStackTrace();
    }
8.对象åºååï¼ç»§æ¿serializableæè Externalizableï¼ï¼å½ä¸ä¸ªå¯¹è±¡å¤æ¬¡è¢«åºååæ¶åªæ第ä¸æ¬¡æ¯çæ£åºååï¼å ¶ä»é½æ¯åªè®°å½å¯¹è±¡ç¼å·ã对äºå¯å对象第ä¸æ¬¡åºååååä¿®æ¹å¯¹è±¡å 容åå¨åºååå½ååºååæ¶ä¿®æ¹çå 容ä¸ä¼çæãå¯ä»¥é对对象è¿è¡åºåååååºååçå®å¶è¾¾å°åªäºå段å¯ä»¥ä¸åºååæè æä¸ä¸ªå¯¹è±¡åºååæå ¶ä»å¯¹è±¡é½å¯ä»¥ï¼å®å ¨äº¤ç»å¼å人åï¼æ¤å¤åªæ¯äºè§£çæ£éè¦åºååæ¶å¨è¯¦ç»æ¥çï¼ã
      //对象åºååå°æ件ï¼æèç¹æµè½¬æ¢æå¤çæµï¼ä¹å¯ä»¥ç¨å ¶ä»IOèç¹æ¥ï¼å¦äºè¿å¶æ°ç»çï¼,
      ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("1.txt"));
      Person per=new Person();
      oos.writeObject(per);
      //读åºå¯¹è±¡
      ObjectInputStream ois=new ObjectInputStream(new FileInputStream("1.txt"));
      Person per2=(Person)ois.readObject();
9.NIOéç¨å åæ å°æ件æ¹å¼å¤çè¾å ¥è¾åºãä¼ ç»çé¢åæµé£NIOåé¢ååã主è¦å ï¼java.nio,java.nio.channels,java.nio.charset,java.nio.channels.spi,java.nio.charset.spiãchannelåbufferæ¯NIOç2æ ¸å¿ãchannelç¸å¯¹äºinputstreamåoutputstreamæ大åºå«æ¯æä¾äºmap()æ¹æ³ï¼è¿æ ·å¯ä»¥ç´æ¥æä¸åæ°æ®æ å°å°å åãnioæä¾charsetç±»æä½å符串å°åèæµçæ¹æ³åæä¾éé»å¡ç¼ç¨çselectorç±»ãå¨java.nioä¸æå¾å¤ç§ bufferå¦ï¼bytebuffer,charbuffer,shortbufferï¼åºæ¬æ°æ®é¤äºboolé½æä¾äºï¼ä½æ¯å¸¸ç¨çæ¯bytebufferåcharbufferï¼ãbufferä¸æflip()åclear()2个æ¹æ³ä¸ä¸ªæ¯ä¸ºåæ°æ®å好åå¤ï¼ä¸ä¸ªæ¯å次åbufferä¸è£ å ¥æ°æ®å好åå¤ï¼ä»¥ä¸è§åï¼
bytebufferè¿æä¸ä¸ªåç±»MappedByteBufferï¼å®æ¯ç±channelçmapæ¹æ³è¿åã并ä¸bytebufferè¿æä¾äºä¸ä¸ªä¸allocate()类似çæ¹æ³allocateDirect()å建ç´æ¥bufferï¼ç´æ¥bufferæ¯æ®ébuffer读åæçæ´é«ï¼ä½æ¯å建ææ¬ä¹å¾å¤§ï¼æ以çæ使ç¨ä¸è¦ç¨ç´æ¥bufferã
10.channelæ¯ç±»ä¼¼æµå¯¹è±¡ï¼å®åªåbufferè¿è¡äº¤äºä¸åç¨åºç´æ¥äº¤äºï¼ç¨åºè¦åå ¥channelå¿ é¡»å åå ¥bufferï¼è¦è®¿é®channelä¹æ¯å è¦å°channelæ°æ®åå ¥å°bufferåç¨åºåç»buffer交äºãchannelå为以ä¸å ç§ï¼DatagramChannelï¼UDPåè®®ï¼ãFileChannelãPipe.SinkChannel,Pipe.SourceChannelï¼çº¿ç¨ä¹é´éä¿¡ï¼ãSelectableChannelãServerSocketChannelãSocketChannelãchannelä¸å¸¸ç¨æ¹æ³æ¯map(),read(),write()ã
以ä¸å±ç¤ºchannelç´æ¥è¯»åæ件ï¼
      File f=new File("1.txt");
      try(
            //å建è¾å ¥æµå¯¹åºçchannel
            FileChannel inchannel=new FileInputStream(f).getChannel();
            //å建è¾åºæµå¯¹åºçchannel
            FileChannel outChannel=new FileOutputStream("2.txt").getChannel()
            )
      {
        Â
         //å°FileChanneléçå ¨é¨æ°æ®æ å°æbytebuffer(å¦ææ件太大å¯ä»¥ä¸ç¨è¿ä¸ªæ¹æ³ï¼æ件太大é æå åä¼æ¶è)ï¼map常ç¨æ¹æ³
         MappedByteBuffer buffer=inchannel.map(FileChannel.MapMode.READ_ONLY, 0, f.length());
         //ç´æ¥å°bufferä¸çæ°æ®å ¨é¨è¾åº
         outChannel.write(buffer);
         //å¤åLimiteåpositionä½ç½®
         buffer.clear();
         //å建解ç å¨
         Charset charset=Charset.forName("GBK");
         //å建解ç å¨å¯¹è±¡
         CharsetDecoder decoder=charset.newDecoder();
         //转æ¢bytebufferå°charbuffer
         CharBuffer charBuffer=decoder.decode(buffer);
         System.out.println(charBuffer);
      }
      catch(IOException ex)
      {
      ex.printStackTrace();
      }
æ¡ä¾äºï¼éä¸æ¬¡æ§è¯»åï¼åä¹å竹篮æ水循ç¯ä¸æ ·ï¼ï¼
      File f=new File("1.txt");
      try(
            //å建è¾å ¥æµå¯¹åºçchannel
            FileChannel fcin=new FileInputStream(f).getChannel()            Â
            )
      {
         //é对ä¸æ¬¡æ§è¯»å大æ件å¯ä»¥ä½¿ç¨ä¼ ç»ç竹篮æ水模å¼
         ByteBuffer bbuff=ByteBuffer.allocate(256);
         //å°filechannelä¸çæ°æ®åæå¤æ¬¡è¯»åå°bbuffä¸
         while(fcin.read(bbuff)!=-1)
         {
            //éå®buffer
            bbuff.flip();
            Charset charset=Charset.forName("GBK");
            CharsetDecoder decoder=charset.newDecoder();
            CharBuffer charBuffer=decoder.decode(bbuff);
            System.out.println(charBuffer);
            bbuff.clear();
         }           Â
      }
      catch(IOException ex)
      {
      ex.printStackTrace();
      }
11.javaæä¾charset对bytebufferåcharbufferä¹é´ç转æ¢
      //è·åç³»ç»ä¸æ¯æçç¼ç
      SortedMap<String, Charset> map = Charset.availableCharsets();
      //解ç
          Charset charset=Charset.forName("GBK");
       CharsetDecoder decoder=charset.newDecoder();
       CharBuffer charBuffer=decoder.decode(bytebuffer);
      //ç¼ç
       Charset charset=Charset.forName("GBK");
       CharsetEncoder ecoder=charset.newEncoder();
       ByteBuffer byteBuffer=ecoder.encode(charbuffer);
12.java7æä¾äºæ件IOåå¼æ¥channelçIOã对äºæ件IOå¨java.nio.fileä¸æä¾æ件æä½ï¼é对æ件æä¾äºPathæ¥å£,PathsåFilesæ¯å·¥å ·ç±»ãéè¦çç»ææ¡filesåpathså·¥å ·ï¼ãå¨java.nio.channelsä¸æä¾äºå¤§éAsynchronouså¼å¤´çchannelæ¥å£åç±»ã
nioæä¾äºjava.nio.file.Files.walk(Path star, FileVisitor<? super Path> visitor)æ¹æ³æ´å¿«æ·çåéæ件ä¸çåæ件æè åç®å½ã
nioæä¾äºæ件夹çæ§çåè½ï¼
WatchService watchService=FileSystems.getDefault().newWatchService();
      Paths.get("c:/").register(watchService,Â
            StandardWatchEventKinds.ENTRY_CREATE,
            StandardWatchEventKinds.ENTRY_DELETE,
            StandardWatchEventKinds.ENTRY_MODIFY);
æä¾fileså çFiles.getFileAttributeView(arg0, arg1, arg2)å¯ä»¥å¯¹æ件è¿ä¸æ¥æä½ã
13.selectorå®ç°channelç注å并å®ç°ä¸å¯¹å¤ç®¡çãå¦ä¸é¢å®ä¾ä»£ç ï¼
Selector selector = Selector.open();//è·åselctor
channel.configureBlocking(false);//注åå¨selectorçchannelå¿ é¡»æ¯éé»å¡ç
SelectionKey key = channel.register(selector, SelectionKey.OP_READ);//注åçchannel被çå¬çäºä»¶ç±»åï¼å¯ä»¥ç¨|çå¬å¤ç§
while(true) {
 int readyChannels = selector.select();//æå¤å°channelå·²ç»å°±ç»ª
 if(readyChannels == 0) continue;//表示没æ就绪channel
 Set selectedKeys = selector.selectedKeys();//è¿å就绪éå
 Iterator keyIterator = selectedKeys.iterator();
 while(keyIterator.hasNext()) {
  SelectionKey key = keyIterator.next();//è¿ä¸ªå¯¹è±¡å æ¬channelãselectorãäºä»¶çå¬ç±»åãæ¥çäºä»¶æ¯å¦å°±ç»ªãå ¶ä»éå 对象
  if(key.isAcceptable()) {
    // a connection was accepted by a ServerSocketChannel.
  } else
if  (key.isConnectable()) {
    // a connection was established with a remote server.
  } else
if  (key.isReadable()) {
    // a channel is ready for reading
  } else
if  (key.isWritable()) {
    // a channel is ready for writing
  }
  keyIterator.remove();
 }
}
14.ByteBufferæ两ç§æ¨¡å¼:ç´æ¥/é´æ¥.é´æ¥æ¨¡å¼æå ¸å(ä¹åªæè¿ä¹ä¸ç§)çå°±æ¯HeapByteBuffer,å³æä½å å å (byte[]).ä½æ¯å åæ¯ç«æé,å¦ææè¦åéä¸ä¸ª1Gçæ件æä¹å?ä¸å¯è½ççå»åé 1Gçå å.è¿æ¶å°±å¿ 须使ç¨âç´æ¥â模å¼,å³ MappedByteBuffer,æ件æ å°.å ä¸æä¸ä¸,è°è°æä½ç³»ç»çå å管ç.ä¸è¬æä½ç³»ç»çå åå两é¨å:ç©çå å;èæå å.èæå åä¸è¬ä½¿ç¨çæ¯é¡µé¢æ åæ件,å³ç¡¬çä¸çæ个(æäº)ç¹æ®çæ件.æä½ç³»ç»è´è´£é¡µé¢æ件å 容ç读å,è¿ä¸ªè¿ç¨å«â页é¢ä¸æ/åæ¢â. MappedByteBufferä¹æ¯ç±»ä¼¼ç,ä½ å¯ä»¥ææ´ä¸ªæ件(ä¸ç®¡æ件æå¤å¤§)çææ¯ä¸ä¸ªByteBuffer.MappedByteBuffer åªæ¯ä¸ç§ç¹æ®çByteBufferï¼å³æ¯ByteBufferçåç±»ã MappedByteBuffer å°æ件ç´æ¥æ å°å°å åï¼è¿éçå åæçæ¯èæå åï¼å¹¶ä¸æ¯ç©çå åï¼ãé常ï¼å¯ä»¥æ å°æ´ä¸ªæ件ï¼å¦ææ件æ¯è¾å¤§çè¯å¯ä»¥å段è¿è¡æ å°ï¼åªè¦æå®æ件çé£ä¸ªé¨åå°±å¯ä»¥ã
15.AIOæä½æ¡ä¾
public static void main(String[] args) throws IOException, InterruptedException {
      //æ¹å¼ä¸å¼æ¥è¯»åæ件æ°æ®(è¿ç§æä½æ¯è¾éº»ç¦ï¼ä¸åçæ¥çæ¯å¦å®æï¼ä¸è¿å¯ä»¥è®©çº¿ç¨å å»å®æå«çäºååæ¥æ¥çæ¯å¦å®æ)
      Path path=Paths.get("C:\\Users\\Administrator\\Desktop\\æ¬å°ç¥è¯\\06 javaå åç³»å\\IO\\test.txt");  Â
      AsynchronousFileChannel fileChannel=AsynchronousFileChannel.open(path, StandardOpenOption.READ);
      ByteBuffer buffer = ByteBuffer.allocate(1024);
      long position = 0;
      Future<Integer> operation = fileChannel.read(buffer, position);
      while(!operation.isDone());
      buffer.flip();
      byte[] data = new byte[buffer.limit()];
      buffer.get(data);
      System.out.println(new String(data));
      buffer.clear();
     Â
     Â
      //æ¹å¼äºå¼æ¥è¯»åæ°æ®(æ¨èç¨æ³ï¼ä»»å¡å®æåä¼èªå·±å¯å¨ä¸ä¸ªçº¿ç¨å¤çIOå®æçæ°æ®)     Â
      System.out.println("主线ç¨ï¼ "+Thread.currentThread().getName());
      fileChannel.read(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() {
        @Override
        public void completed(Integer result, ByteBuffer attachment) {          Â
           System.out.println("åè°çº¿ç¨ï¼ "+Thread.currentThread().getName());            Â
          attachment.flip();
          byte[] data = new byte[attachment.limit()];
          attachment.get(data);
          System.out.println(new String(data));
          attachment.clear();
        }
        @Override
        public void failed(Throwable exc, ByteBuffer attachment) {
        }
      });  Â
   }
16.é对windowsçå¼æ¥socketï¼sunæä¾äºä¸é¨çç±»å¤çï¼å¦ï¼WindowsAsynchronousFileChannelImpl