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

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案(14)

          文章分類(18)

          文章檔案(18)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 青海省| 林周县| 泉州市| 景宁| 柞水县| 堆龙德庆县| 阳朔县| 洪泽县| 昭觉县| 会泽县| 阿克陶县| 海安县| 枝江市| 柳江县| 云阳县| 江川县| 会东县| 尉氏县| 克东县| 彭水| 舟曲县| 弥勒县| 万安县| 五家渠市| 察隅县| 济源市| 博湖县| 礼泉县| 德安县| 兴隆县| 巩留县| 晋城| 万宁市| 道真| 阳高县| 卢湾区| 九龙县| 石屏县| 师宗县| 大方县| 威远县|