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. centos 怎么安装 g++

    centos 怎么安装 g++ 找了n久  找到一个实用的 有gcc  但是 是老版本的  tarball 编译 nmap 的时候说机器没有g++ 各种方法都试过 然后 找到下面这个方法: cento ...

  2. sql常用语句

    选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delet ...

  3. [阅读笔记]Software optimization resources

    http://www.agner.org/optimize/#manuals 阅读笔记Optimizing software in C++   7. The efficiency of differe ...

  4. AX2012 QTY小数的位数问题

    小数的位数 ============================================ A.扩展数据类型Qty的位数(默认为2位)B.单位中的位数(通常PCS为0位)C.扩展数据类型Bo ...

  5. 输入scanner,if-else

    import java.util.Scanner; public class Job{ public static void main(String[] args){ Scanner scanner ...

  6. 反斜杠在JSP中的两种不同的含义

    / 在不同条件下的不同含义 / 代表WEB应用的根路径的情况:/ 交给 Servlet容器来处理 请求转发时: request.getRequestDispatcher("/xxxx&quo ...

  7. javascript中运算符的优先级

    运算符优先级 JavaScript中的运算符优先级是一套规则.该规则在计算表达式时控制运算符执行的顺序.具有较高优先级的运算符先于较低优先级的运算符执行.例如,乘法的执行先于加法. 下表按从最高到最低 ...

  8. Linux(Ubuntu 14.04) setting up OpenGL

    1. Install c/c++ compilation package. 2. install openGL and freeGlut library sudo apt-get install me ...

  9. TypeError: window.open is not a function

    想必大家现在都已经到家了,而苦逼的我还要坐在办公室混拿微薄的工资,技不如人,平常不努力给自己充电,年终一毛钱都没多给.不说这扫兴的话题了,在这给同样在苦逼坚守岗位的同志们节日的问候,新的一年,好运连连 ...

  10. C语言模块化编译介绍

    C语言模块化编译介绍 模块化编程的概念 所谓模块化变成(多文件开发),就是多文件(.c文件)编程,一个.c文件和一个.h文件可以被称为一个模块. 头文件开发的注意事项: 1)头文件中可以和C程序一样引 ...