乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      FileChannel類的簡單用法_kj,jh

       Blex 2011-03-21

      清單一:

      import java.io.*;
      import java.nio.*;
      import java.nio.channels.*;
      public class GetChannel
      {
      private static final int BSIZE = 1024;
      public static void main(String[] args)throws IOException
      {
          FileChannel fc = new FileOutputStream ("data.txt").getChannel();
          fc.write(ByteBuffer.wrap("some txt".getBytes()));//write()    Writes a sequence of bytes to
          //this channel from the given buffer
          fc.close();
          fc = new RandomAccessFile("data.txt","rw").getChannel();
          fc.position(fc.size());//abstract    FileChannel position(long newPosition)
                                   //Sets this channel's file position.
          fc.write(ByteBuffer.wrap("some more".getBytes()));
          fc.close();
          fc =new FileInputStream("data.txt").getChannel();
          ByteBuffer bf =    ByteBuffer.allocate(BSIZE);//static ByteBuffer allocate(int capacity)
                                                          //Allocates a new byte buffer.
          //一旦調(diào)用read()來告知FileChannel向ByteBuffer存儲字節(jié),就必須調(diào)用緩沖器上的filp(),
          //讓它做好別人存儲字節(jié)的準備(是的,他是有點拙劣,但請記住他是很拙劣的,但卻適于獲取大速度)
          //
          fc.read(bf);// Reads a sequence of bytes from this channel into the given buffer
          bf.flip();
          while(bf.hasRemaining())
              System.out.print((char)bf.get());
      }
      }

       

      清單二:

      //Copying a file using channels and buffers;
      import java.io.*;
      import java.nio.*;
      import java.nio.channels.*;
      public class ChannelCopy
      {
      private static final int BSIZE = 1024;
      public static void main(String [] args)throws IOException
      {
         if (args.length!=2)
         {
          System.out.println("argument:sourcefile destfile");
          System.exit(1);
         }
         FileChannel
              in = new FileInputStream (args[0]).getChannel(),
              out = new FileOutputStream (args[1]).getChannel();
         ByteBuffer bb = ByteBuffer.allocate(BSIZE);
         while (in.read(bb)!=-1)
         {
          bb.flip();
          out.write(bb);
          bb.clear();//prepare for reading;清空緩沖區(qū);
         }
      }
      }

       

      清單三:

      import java.io.*;
      import java.nio.*;
      import java.nio.channels.*;
      public class TransferTo
      {
      public static void main(String [] args) throws IOException
      {
         if(args.length!=2)
         {
          System.out.println("argument: sourcefile destfile");
          System.exit(1);
         }
         FileChannel
             in = new FileInputStream(args[0]).getChannel(),
             out = new FileOutputStream(args[1]).getChannel();
      //abstract   long transferTo(long position, long count, WritableByteChannel target)
      //          Transfers bytes from this channel's file to the given writable byte channel.
         in.transferTo(0,in.size(),out);
         //or
         //using out
      //abstract   long transferFrom(ReadableByteChannel src, long position, long count)
            //      Transfers bytes into this channel's file from the given readable byte channel.
      // out.transferFrom(in,0,in.size());
      }
      }

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多