Channel必须要通过buffer来读写

1. Channel需要通过IO流的getChannel()方法获取

2. buffer需要通过Channel的map()方法获取

package com.io.channel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder; public class ChannelB { public static void main(String[] args) { String path = "E:\\fitnesse\\workspace\\javaio\\resource\\";
String nameFilePath = path + "name.txt";
String copyFilePath = path + "copy.txt"; File nf = new File(nameFilePath);
File cf = new File(copyFilePath); //第一步获取Channel,有很多种Channl,这里使用到的是FileChannel
try(FileChannel inChannel = new FileInputStream(nf).getChannel();
FileChannel outChannel = new FileOutputStream(cf).getChannel())
{
//从name.txt中读取内容到buffer中
MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY , 0 , nf.length()); //将从name.txt中读取的内容写入到copy.txt
outChannel.write(buffer);
buffer.clear(); //设置编码和解码器
Charset charset = Charset.forName("GBK");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(buffer);
System.out.println(charBuffer); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} }

name.txt文件内容

执行程序后输出的内容

1231542155112221
45444545454454545
41511111111111111111111111111111
88888888888888888888888888888888888888

copy.txt的内容

Channel的使用的更多相关文章

  1. Golang, 以17个简短代码片段,切底弄懂 channel 基础

    (原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...

  2. TODO:Go语言goroutine和channel使用

    TODO:Go语言goroutine和channel使用 goroutine是Go语言中的轻量级线程实现,由Go语言运行时(runtime)管理.使用的时候在函数前面加"go"这个 ...

  3. GO语言之channel

    前言: 初识go语言不到半年,我是一次偶然的机会认识了golang这门语言,看到他简洁的语法风格和强大的语言特性,瞬间有了学习他的兴趣.我是很看好go这样的语言的,一方面因为他有谷歌主推,另一方面他确 ...

  4. Critical: Update Your Windows Secure Channel (cve-2014-6321,MS14-066)

    前言:风雨欲来山满楼,下半年开始各种凶猛的漏洞层出不穷,天下已经不太平,互联网已经进入一个新的台阶 0x01 cve-2014-6321 11月的补丁月,微软请windows的用户吃了顿大餐,发布了1 ...

  5. JAVA NIO Channel

    Basic:   多数通道都是链接到开发的文件描述符的.Channel类提供维持平台独立性的抽象过程.   通道是一种途径,访问和操作操作系统,缓冲区是数据操作点: Channel类继承结构图: 通过 ...

  6. channel Golang

    Golang, 以17个简短代码片段,切底弄懂 channel 基础 (原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的 ...

  7. go channel

    channel 是go语言中不同goroutine之间通信一种方法 //定义一个channel c := make(chan bool) //向channel中写入 c <- true //读取 ...

  8. [bigdata] flume file channel CPU消耗比 memory channel高的原因

    https://www.quora.com/Why-does-flume-take-more-resource-CPU-when-file-channel-is-used-compared-to-wh ...

  9. 图解Netty之Pipeline、channel、Context之间的数据流向。

    声明:本文为原创博文,禁止转载.       以下所绘制图形均基于Netty4.0.28版本. 一.connect(outbound类型事件)  当用户调用channel的connect时,会发起一个 ...

  10. go:channel(未完)

    注:1)以下的所有讨论建立在包含整形元素的通道类型之上,即 chan int 2)对于“<-”我的理解是,它可能是一个操作符(接收操作符),也  可能是类型的一部分(如“chan<- in ...

随机推荐

  1. caffe搭建--ubuntu标准平台的搭建

    http://caffe.berkeleyvision.org/install_apt.html Ubuntu Installation General dependencies sudo apt-g ...

  2. erlang实现DNS解析域名

    erlang的DNS解析,有个inet模块,可以做这个事情,对应的api如下:(这里用的R16B的版本) inet:getaddr/2解析对应一个ip,而inet:getaddr/2解析有可能对应1个 ...

  3. 【iOS开发】---- UIView动画

      iOS 动画UIView动画 原文:http://www.cocoachina.com/bbs/read.php?tid=110168 1.概述 UIKit直接将动画集成到UIView类中,实现简 ...

  4. Cena使用

    打开cena,在工具-选项中,修改G++和GCC的编译命令.格式:[g++目录]g++.exe %s.cpp -o %s.exe [编译选项]例如以下命令使用刚安装的mingw4.8.1 g++编译, ...

  5. Hibernater中的持久化对象以及状态转换

    一.持久化对象的要求 1.提供一个无参的构造器:使Hibernate可以使用Constructor.newInstance() 来实例化持久化类. 2.提供一个标识属性(identifier prop ...

  6. 图片转base64存储

    图片转base64存储 base64.b64encode(r.content) url='http://www.heze.cn/info/themes/heze/Public/tel/?tel=MDU ...

  7. Two-Factor Authentication 2FA

    About two-factor authentication - User Documentation https://help.github.com/articles/about-two-fact ...

  8. RequestDispatcher用法

    1.具体用法: RequestDispatcher dispatcher = req.getRequestDispatcher("updateByUserId_001.jsp"); ...

  9. 【转】hibernate懒加载的问题,failed to lazily initialize a collection of role

    hibernate懒加载的问题,failed to lazily initialize a collection of role hibernate懒加载的问题,failed to lazily in ...

  10. awk 根据外部变量匹配某一域值

    shell>> i='a' awk '$1 ~ /'$i'/ {print $0}' test.txt awk中,变量 增加单引号即可