New I/O

          Channel & ByteBuffer
           channal & buffer - ByteBuffer
          package think.in.java.io;
          /**
           * That are closer to the operating system’s way of performing I/O: channels and buffers.
           * The only kind of buffer that communicates directly with a channel is a ByteBuffer—that is, 
           * a buffer that holds raw bytes.
           * 
           * It’s fairly low-level, precisely because this makes a more efficient mapping with most operating systems.
           * 
           * Here’s a simple example that exercises all three types of stream 
           * to produce channels that are writeable, read/writeable, and readable:
           * 
          @author WPeng
           *
           * 
          @since 2012-11-6
           
          */
          public class GetChannel {

              
          private static final int BSIZE = 1024;

              
          public static void main(String[] args) throws Exception {
                  
          // Write a file
                  FileChannel fc = new FileOutputStream("data.txt").getChannel();
                  fc.write(ByteBuffer.wrap(
          "Some text".getBytes()));
                  fc.close();
                  
                  
          // Add to the end of the file
                  fc = new RandomAccessFile("data.txt""rw").getChannel();
                  fc.position(fc.size());  
          //move to the end
                  fc.write(ByteBuffer.wrap("Some more".getBytes()));
                  fc.close();
                  
                  
          // Read the file
                  fc = new FileInputStream("data.txt").getChannel();
                  ByteBuffer buff 
          = ByteBuffer.allocate(BSIZE);
                  
          /**
                      Reads a sequence of bytes from this channel into the given buffer.
                      Bytes are read starting at this channel's current file position,
                      and then the file position is updated with the number of bytes actually read.

                      -1 if the channel has reached end-of-stream
                  
          */
                  fc.read(buff);
                  buff.flip();
                  
          while(buff.hasRemaining()){
                      System.out.print((
          char)buff.get());
                  }
              }

          }
          transferTo & transferFrom
          package think.in.java.io;
          /**
           * Using transferTo() between channels 
           * {Args: TransferTo.java TransferTo.txt}
           * 
          @author WPeng
           *
           * 
          @since 2012-11-6
           
          */
          public class TransferTo {
              
          public static void main(String[] args) throws IOException{
                  
          if(args.length != 2){
                      System.out.println(
          "arguments: sourcefile destfile");
                      System.exit(
          1);
                  }
                  FileChannel in 
          = new FileInputStream(args[0]).getChannel(),
                              out 
          = new FileOutputStream(args[1]).getChannel();
                  in.transferTo(
          0, in.size(), out);
                  
          // or:
                  
          // out.transferFrom(in, 0, in.size());
              }
          }









          posted on 2012-11-06 14:42 鹽城小土包 閱讀(122) 評論(0)  編輯  收藏 所屬分類: J2EE

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案(14)

          文章分類(18)

          文章檔案(18)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 海宁市| 来宾市| 九龙坡区| 阿拉善右旗| 佳木斯市| 夏邑县| 苏州市| 佛山市| 星子县| 雅安市| 博客| 万荣县| 博白县| 江油市| 富宁县| 桃园市| 中宁县| 景谷| 格尔木市| 陵川县| 新竹市| 互助| 祥云县| 千阳县| 余庆县| 绥芬河市| 长海县| 四平市| 东台市| 探索| 荥阳市| 杭州市| 石泉县| 和政县| 嘉定区| 秭归县| 手机| 分宜县| 白玉县| 镇巴县| 沙河市|