在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java的nio包的简单使用

2016-7-29 15:47| 发布者: zhangjf| 查看: 689| 评论: 0

摘要: java的nio包的简单使用 public class GetChannel {private static final int BSIZE = 1024;public static void main(String args) throws IOException { FileChannel fc = new FileOutputStream(./s ...
java的nio包的简单使用
                   
            
  1. public class GetChannel {
  2.   private static final int BSIZE = 1024;
  3.   public static void main(String[] args) throws IOException {
  4.     FileChannel fc = new FileOutputStream("./src/test/java/chapter18/work23/data.txt").getChannel();
  5.     fc.write(ByteBuffer.wrap("Some text".getBytes()));
  6.     fc.close();
  7.     fc = new RandomAccessFile("./src/test/java/chapter18/work23/data.txt", "rw").getChannel();
  8.     fc.position(fc.size());
  9.     fc.write(ByteBuffer.wrap("Some more".getBytes()));
  10.     fc.close();
  11.     fc = new FileInputStream("./src/test/java/chapter18/work23/data.txt").getChannel();
  12.     ByteBuffer buff = ByteBuffer.allocate(BSIZE); // 分配BSIZE的“卡车”
  13.     fc.read(buff);  // 从“通道”向卡车中装载数据
  14.     buff.flip();  // 准备buff对象以供write()方法写数据
  15.     while (buff.hasRemaining()) {
  16.       System.out.print(((char) buff.get()));
  17.     }
  18.   }
  19. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 15:53

Copyright 2015-2025 djqfx

返回顶部