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. 【转载】 input 输入格式化

    不多说直接 发链接 http://nosir.github.io/cleave.js/

  2. 2014最后一天,好烦!这个问题从来没遇到过!网上查找了很多办法都没解决!并且no wifi 了!

    org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread ...

  3. 网页qq客服代码并自定义图片

    <script>var online= new Array();</script> <script src="http://webpresence.qq.com ...

  4. CSS3动画特效——transform详解

    transform让css3可以做很优质的特效,transform的意思是:改变,使-变形,转换. 在css3中transform的作用也是改变,让元素倾斜,旋转,缩放,位移. 下面来一一分解tran ...

  5. C++ STL vector容器学习

    STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...

  6. Asp.Net_Mvc_获取当前Url、Controller、Action

    一.URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟 ...

  7. easymock所测试的方法内部新NEW对象的处理

    问题:当记录的方法的参数是方法所在类内部新NEW的对象时,静态的记录方法交互就会失效,例如 调用的方法: public calss A{ public void method(User u){ u.s ...

  8. error CS0117: `UnityEditor.EditorUtility' does not contain a definition for `GetAssetPreview'

    have to replace: EditorUtility by AssetPreview

  9. SSIS-包调用包错误的解决方案

    1.错误信息: 无法解密受保护的 XML 节点“ DTS:Password”,错误为 0x8009000B“该项不适于在指定状态下使用.”.可能您无权访问此信息.当发生加密错误时会出现此错误.请确保提 ...

  10. AxureRP8实战手册(基础31-40)

    AxureRP8实战手册(基础31-40) 本文目录 基础31.     切换元件库 第2章          页面设置 基础32.     设置页面居中 基础33.     设置页面背景(图片/颜色 ...