a.txt

孔雀向西飞,今朝更好看。
孔雀向西飞,今朝更好看。
孔雀向西飞,今朝更好看。
孔雀向西飞,今朝更好看。

示例一、

 package com.test;

 import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; public class Main {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("E://a.txt");
FileOutputStream fos = new FileOutputStream("E://b.txt");
FileChannel in = fis.getChannel();
FileChannel out = fos.getChannel();
ByteBuffer buf = ByteBuffer.allocateDirect(20);
while (true) {
// 从通道in中读数据到buf,然后从buf中读出来写入out通道
int eof = in.read(buf);
if (eof == -1)
break;
buf.flip();
int c = out.write(buf);
System.out.println("c=" + c);
buf.clear();
}
fis.close();
fos.close(); }
} 执行结果
b.txt 孔雀向西飞,今朝更好看。
孔雀向西飞,今朝更好看。
孔雀向西飞,今朝更好看。
孔雀向西飞,今朝更好看。 示例二、
 package com.test;

 import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; public class Main {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("E://a.txt");
FileOutputStream fos = new FileOutputStream("E://b.txt");
FileOutputStream fos2 = new FileOutputStream("E://c.txt");
FileChannel in = fis.getChannel();
FileChannel out = fos.getChannel();
FileChannel out2 = fos2.getChannel();
ByteBuffer buf = ByteBuffer.allocateDirect(20);
ByteBuffer buf2 = ByteBuffer.allocateDirect(20);
while (true) {
// 从通道in中读数据到buf,然后从buf中读出来写入out通道
int eof = in.read(buf);
if (eof == -1)
break;
buf.flip();
int c = out.write(buf);
System.out.println("c=" + c);
buf.clear();
// 从通道in中读数据到buf,然后从buf中读出来写入out2通道
int eof2 = in.read(buf2);
if (eof2 == -1)
break;
buf2.flip();
int c2 = out2.write(buf2);
System.out.println("c2=" + c2);
buf2.clear();
}
fis.close();
fos.close();
fos2.close();
}
}

执行结果

b.txt

孔雀向西飞,今朝更好朝更好看。
孔雀向西雀向西飞,今朝更好看

c.txt

看。
孔雀向西飞,今飞,今朝更好看。
孔。


Buffer、Channel示例的更多相关文章

  1. Netty那点事: 概述, Netty中的buffer, Channel与Pipeline

    Netty那点事(一)概述 Netty和Mina是Java世界非常知名的通讯框架.它们都出自同一个作者,Mina诞生略早,属于Apache基金会,而Netty开始在Jboss名下,后来出来自立门户ne ...

  2. JAVA基础知识之NIO——Buffer.Channel,Charset,Channel文件锁

    NIO机制 NIO即NEW IO的意思,是JDK1.4提供的针对旧IO体系进行改进之后的IO,新增了许多新类,放在java.nio包下,并对java.io下许多类进行了修改,以便使用与nio. 在ja ...

  3. NIO的Buffer&Channel&Selector

    java的NIO和AIO Buffer position.limit.capacity 初始化 Buffer 填充 Buffer 提取 Buffer 中的值 mark() & reset() ...

  4. NIO之Buffer操作示例

    1. buffer常规操作 略 2. 只读buffer /** * 只读buffer */ public class BufferTest01 { public static void main(St ...

  5. NIO Channel和Buffer

    Java NIO 由以下几个核心部分组成: Buffer Channel Selector 传统的IO操作面向数据流,意味着每次从流中读一个或多个字节,直至完成,数据没有被缓存在任何地方.NIO操作面 ...

  6. 《精通并发与Netty》学习笔记(10 - 详解NIO (一) Channel、Buffer )

    一.Java NIO 概述 Java NIO 由以下几个核心部分组成:ChannelsBuffersSelectors虽然Java NIO 中除此之外还有很多类和组件,但在我看来,Channel,Bu ...

  7. go 技巧: 实现一个无限 buffer 的 channel

    前言 总所周知,go 里面只有两种 channel,一种是 unbuffered channel, 其声明方式为 ch := make(chan interface{}) 另一种是 buffered ...

  8. 转:Java NIO系列教程(二) Channel

    Java NIO的通道类似流,但又有些不同: 既可以从通道中读取数据,又可以写数据到通道.但流的读写通常是单向的. 通道可以异步地读写. 通道中的数据总是要先读到一个Buffer,或者总是要从一个Bu ...

  9. java Channel

    Channel Channel与流 基本上,所有的 IO 在NIO 中都从一个Channel 开始.Channel 有点象流.数据可以从Channel读到Buffer中,也可以从Buffer 写到Ch ...

随机推荐

  1. 腾讯云>>云通信>>TLS后台API在mac上JAVA DEMO搭建

    1.相关文档地址 2.相关demo代码 代码部分作了修改,使用了commons-io中的IOUtils.toString简化了io操作 public class Demo { public stati ...

  2. 译\Node.js应用的持续部署

    Node.js应用的持续部署 翻译前 翻译自:https://blog.risingstack.com/continuous-deployment-of-node-js-applications/ 正 ...

  3. JAVA 1.2(原生数据类型 Primitive Data Type)

    1. Java的数据类型分为2类 >> 原生数据类型(primitive data type) >> 引用数据类型(reference data type) 3. 常量和变量 ...

  4. ubuntu apt-get 时 Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

    sudo cp /etc/apt/sources.list ~/ sudo wget "http://pastebin.com/raw.php?i=uzhrtg5M" -O /et ...

  5. 使用automake等命令自动生成Makefile文件 (转载)

    使用automake等命令自动生成Makefile文件   Linux下编程时,为了方便编译,往往使用Makefile文件自动完成编译,但是Makefile文件本身的书写十分复杂,规则很多.好在Lin ...

  6. 数据挖掘算法(四)Apriori算法

    参考文献: 关联分析之Apriori算法

  7. excel读取

    一.jar包 二.工具类 package excel; import java.io.FileInputStream; import java.io.FileNotFoundException; im ...

  8. 组件嵌套+Mixin函数demo

    非DOM属性:1.dangerouslysetInnerHTML(xxs跨站攻击) 2.key 3.ref 编写组件嵌套 <!DOCTYPE html><html>  < ...

  9. linux发行版基础目录

    linux发行版基础目录 linux 基础目录 linux基础目录 目录 作用 / 根目录,起源 /boot linux引导启动目录 /lib 库目录 /bin 常用内部命令 /sbin 常用内部管理 ...

  10. 十天学会DIV+CSS(DIV布局)

    一列布局: 一列固定宽度.一列固定宽度居中.一列自适应宽度.一列自适应宽度居中 一列固定宽度 <head> <style type="text/css"> ...