`
xzknet
  • 浏览: 300239 次
  • 性别: Icon_minigender_1
  • 来自: 河南
文章分类
社区版块
存档分类
最新评论

FileChannel类的简单用法

阅读更多

清单一:

  1. import java.io.*;
  2. import java.nio.*;
  3. import java.nio.channels.*;
  4. public class GetChannel
  5. {
  6. private static final int BSIZE = 1024;
  7. public static void main(String[] args)throws IOException 
  8. {
  9.     FileChannel fc = new FileOutputStream ("data.txt").getChannel();
  10.     fc.write(ByteBuffer.wrap("some txt".getBytes()));//write()    Writes a sequence of bytes to 
  11.     //this channel from the given buffer
  12.     fc.close();
  13.     fc = new RandomAccessFile("data.txt","rw").getChannel();
  14.     fc.position(fc.size());//abstract    FileChannel position(long newPosition) 
  15.                              //Sets this channel's file position. 
  16.     fc.write(ByteBuffer.wrap("some more".getBytes()));
  17.     fc.close();
  18.     fc =new FileInputStream("data.txt").getChannel();
  19.     ByteBuffer bf =    ByteBuffer.allocate(BSIZE);//static ByteBuffer allocate(int capacity) 
  20.                                                     //Allocates a new byte buffer. 
  21.     //一旦调用read()来告知FileChannel向ByteBuffer存储字节,就必须调用缓冲器上的filp(),
  22.     //让它做好别人存储字节的准备(是的,他是有点拙劣,但请记住他是很拙劣的,但却适于获取大速度)
  23.     //
  24.     fc.read(bf);// Reads a sequence of bytes from this channel into the given buffer
  25.     bf.flip();
  26.     while(bf.hasRemaining())
  27.         System.out.print((char)bf.get());
  28. }
  29. }

 

清单二:

  1. //Copying a file using channels and buffers;
  2. import java.io.*;
  3. import java.nio.*;
  4. import java.nio.channels.*;
  5. public class ChannelCopy
  6. {
  7. private static final int BSIZE = 1024;
  8. public static void main(String [] args)throws IOException 
  9. {
  10.    if (args.length!=2)
  11.    {
  12.     System.out.println("argument:sourcefile destfile");
  13.     System.exit(1);
  14.    }
  15.    FileChannel 
  16.         in = new FileInputStream (args[0]).getChannel(),
  17.         out = new FileOutputStream (args[1]).getChannel();
  18.    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
  19.    while (in.read(bb)!=-1)
  20.    {
  21.     bb.flip();
  22.     out.write(bb);
  23.     bb.clear();//prepare for reading;清空缓冲区;
  24.    }
  25. }
  26. }

 

清单三

  1. import java.io.*;
  2. import java.nio.*;
  3. import java.nio.channels.*;
  4. public class TransferTo
  5. {
  6. public static void main(String [] args) throws IOException
  7. {
  8.    if(args.length!=2)
  9.    {
  10.     System.out.println("argument: sourcefile destfile");
  11.     System.exit(1);
  12.    }
  13.    FileChannel
  14.        in = new FileInputStream(args[0]).getChannel(),
  15.        out = new FileOutputStream(args[1]).getChannel();
  16. //abstract   long transferTo(long position, long count, WritableByteChannel target) 
  17. //          Transfers bytes from this channel's file to the given writable byte channel. 
  18.    in.transferTo(0,in.size(),out);
  19.    //or
  20.    //using out
  21. //abstract   long transferFrom(ReadableByteChannel src, long position, long count) 
  22.       //      Transfers bytes into this channel's file from the given readable byte channel. 
  23. // out.transferFrom(in,0,in.size());
  24. }
  25. }
分享到:
评论
1 楼 一塌糊涂 2011-11-10  
本来我不想评论的,
但是我实在觉得在网上转转来实在是没意思。
你既然转了,干吗他妈的边转载出处都不写!
蛋疼!

相关推荐

Global site tag (gtag.js) - Google Analytics