import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; //=================================================
// File Name : FileChannel_demo
//------------------------------------------------------------------------------
// Author : Common //主类
//Function : FileChannel_demo
public class FileChannel_demo { public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
String info[] = {"123","456","789"};
File f = new File("/home/common/software/coding/HelloWord/HelloWord/out.txt");//路径
FileOutputStream output = null;
output = new FileOutputStream(f);
FileChannel fout = null; //声明输出的通道
fout = output.getChannel(); //得到输出的文件通道
ByteBuffer buf = ByteBuffer.allocate(1024); //开辟缓冲
for(int i=0;i<info.length;i++){
buf.put(info[i].getBytes());
}
buf.flip(); //重设缓冲区,准备输出
fout.write(buf); //输出
fout.close();
output.close(); } }

 读写文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; //=================================================
// File Name : FileChannel_demo
//------------------------------------------------------------------------------
// Author : Common //主类
//Function : FileChannel_demo
public class FileChannel_demo { public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根 File f1 = new File("/home/common/software/coding/HelloWord/HelloWord/out.txt");//路径
File f2 = new File("/home/common/software/coding/HelloWord/HelloWord/outnote.txt");//路径
FileInputStream input = null;
FileOutputStream output = null;
input = new FileInputStream(f1);
output = new FileOutputStream(f2);
FileChannel fin = null; //声明输入的通道
FileChannel fout = null; //声明输出的通道
fin = input.getChannel(); //得到输入的文件通道
fout = output.getChannel(); //得到输出的文件通道
ByteBuffer buf = ByteBuffer.allocate(1024); //开辟缓冲
int temp = 0; //声明变量接收内容
while((temp=fin.read(buf)) != -1){
buf.flip();
fout.write(buf);
buf.clear();
}
fin.close();
fout.close();
input.close();
output.close(); } }

Java——新IO 通道的更多相关文章

  1. Java 新IO

       NIO提供全新的底层I/O模型.与最初的java.io包中面向流(stream-oriented)概念不同,NIO采用了面向块的概念(block-oriented).在尽可能的情况下,I/O的操 ...

  2. Java -- 新IO -- 目录

    20.1 Java 新IO简介 20.2 缓冲区与Buffer 例:演示缓冲区的操作流程 Class : IntBufferDemo01 20.2.2 深入缓冲区操作 20.2.3 创建子缓冲区 20 ...

  3. 怎样获取java新IO的Path文件大小

    import org.junit.Test; import java.io.IOException; import java.nio.file.Files; import java.nio.file. ...

  4. Java——新IO 缓冲区与Buffer

    缓冲区和Buffer import java.nio.IntBuffer; //================================================= // File Na ...

  5. JAVA(六)数据库/网络编程/新IO

    成鹏致远 | lcw.cnblog.com |2014-02-05 数据库 1.JDBC概述 JDBC(Java Database Connectivity,Java数据库连接)提供了一种与平台无关的 ...

  6. java 21 - 15 新IO流 NIO

    1:JDK4  新IO要了解的类 Buffer(缓冲),Channer(通道) 2:JDK7  要了解的新IO类 Path:与平台无关的路径. Paths:包含了返回Path的静态方法. public ...

  7. Java 8特性尝鲜:新新IO

    Java 8特性尝鲜:新新IO 在这个专题前面的文章中,我们已经看到,使用Java8的lambda表达式对现有的JDK1.2 I/O库的提升,主要是可以使用lambda表达式来构造java.io.Fi ...

  8. Java NIO之通道

    一.前言 前面学习了缓冲区的相关知识点,接下来学习通道. 二.通道 2.1 层次结构图 对于通道的类层次结构如下图所示. 其中,Channel是所有类的父类,其定义了通道的基本操作.从 Channel ...

  9. 【NIO】Java NIO之通道

    一.前言 前面学习了缓冲区的相关知识点,接下来学习通道. 二.通道 2.1 层次结构图 对于通道的类层次结构如下图所示. 其中,Channel是所有类的父类,其定义了通道的基本操作.从 Channel ...

随机推荐

  1. C#中的interface

    接口(interface) 接口泛指实体把自己提供给外界的一种抽象化物(可以为另一实体),用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的方式. 接口实际上是一个约定: ...

  2. jquery使用淘宝接口跨域查询手机号码归属地实例

    <h1>手机号码归属地查询</h1>    <div class="outer">        <p>请输入手机号码</p& ...

  3. 【BZOJ 1038】【ZJOI 2008】瞭望塔

    http://www.lydsy.com/JudgeOnline/problem.php?id=1038 半平面交裸题,求完半平面后在折线段上的每个点竖直向上和半平面上的每个点竖直向下求距离,统计最小 ...

  4. 使用AOP 实现多数据源 切换

    多数据源的实现,这里就来个实例吧 1.在 spring 的配置文件中数据源信息 <?xml version="1.0" encoding="UTF-8"? ...

  5. Freemarker list标签,list数据判断使用

    <#if listSecMDIF?exists && (listSecMDIF?size>0)> <ul style="float: left;mar ...

  6. 精通Web Analytics 2.0 (2) 内容简介

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 这本书里有什么? 这本书是在我第一本书<网站分析:每天一小时>的基础上创作的.我并不打算绕弯子,第一章是网站分析2. ...

  7. xargs 命令

    1. xargs 很好用,可以将多个结果分段传给下一个命令进行计算 比如说find 找到很多个文件,又想对每个文件统计条数: find 20151201 -name "mjoys*.txt& ...

  8. Leetcode Bulb Switcher

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  9. codeforces 723A : The New Year: Meeting Friends

    Description There are three friend living on the straight line Ox in Lineland. The first friend live ...

  10. TortoiseGit与GitHub项目关联设置

    一.常规克隆GitHub上的项目: 1.在本地硬盘上放置项目的地方上[右键]->[Git 克隆]->在[url地址]上输入https的GitHub的链接,然后就是等待完成,之后即可完成拉取 ...