1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.nio.ByteBuffer;
  4. import java.nio.channels.FileChannel;
  5.  
  6. //=================================================
  7. // File Name : FileChannel_demo
  8. //------------------------------------------------------------------------------
  9. // Author : Common
  10.  
  11. //主类
  12. //Function : FileChannel_demo
  13. public class FileChannel_demo {
  14.  
  15. public static void main(String[] args) throws Exception{
  16. // TODO 自动生成的方法存根
  17. String info[] = {"123","456","789"};
  18. File f = new File("/home/common/software/coding/HelloWord/HelloWord/out.txt");//路径
  19. FileOutputStream output = null;
  20. output = new FileOutputStream(f);
  21. FileChannel fout = null; //声明输出的通道
  22. fout = output.getChannel(); //得到输出的文件通道
  23. ByteBuffer buf = ByteBuffer.allocate(1024); //开辟缓冲
  24. for(int i=0;i<info.length;i++){
  25. buf.put(info[i].getBytes());
  26. }
  27. buf.flip(); //重设缓冲区,准备输出
  28. fout.write(buf); //输出
  29. fout.close();
  30. output.close();
  31.  
  32. }
  33.  
  34. }

 读写文件

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.nio.ByteBuffer;
  5. import java.nio.channels.FileChannel;
  6.  
  7. //=================================================
  8. // File Name : FileChannel_demo
  9. //------------------------------------------------------------------------------
  10. // Author : Common
  11.  
  12. //主类
  13. //Function : FileChannel_demo
  14. public class FileChannel_demo {
  15.  
  16. public static void main(String[] args) throws Exception{
  17. // TODO 自动生成的方法存根
  18.  
  19. File f1 = new File("/home/common/software/coding/HelloWord/HelloWord/out.txt");//路径
  20. File f2 = new File("/home/common/software/coding/HelloWord/HelloWord/outnote.txt");//路径
  21. FileInputStream input = null;
  22. FileOutputStream output = null;
  23. input = new FileInputStream(f1);
  24. output = new FileOutputStream(f2);
  25. FileChannel fin = null; //声明输入的通道
  26. FileChannel fout = null; //声明输出的通道
  27. fin = input.getChannel(); //得到输入的文件通道
  28. fout = output.getChannel(); //得到输出的文件通道
  29. ByteBuffer buf = ByteBuffer.allocate(1024); //开辟缓冲
  30. int temp = 0; //声明变量接收内容
  31. while((temp=fin.read(buf)) != -1){
  32. buf.flip();
  33. fout.write(buf);
  34. buf.clear();
  35. }
  36. fin.close();
  37. fout.close();
  38. input.close();
  39. output.close();
  40.  
  41. }
  42.  
  43. }

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. PHP之function_handling 函数

    function_handling 函数 function_handling 函数 需求: 编写一个函数,传入的参数个数不确定,请求出其和. 使用到 以下几个函数: 代码说明: 函数的参数可以是另外一 ...

  2. Mybatis 异常: The content of elements must consist of well-formed character data or markup

    原因很简单:在ibatis的配置文件中不能出现小于号(>)     <delete id="deleteByPrimaryKey" parameterType=&quo ...

  3. Chrome开发工具Elements面板(编辑DOM和CSS样式)详解

    Element 译为“元素”,Element 面板可以让我们动态查看和编辑DOM节点和CSS样式表,并且立即生效,避免了频繁切换浏览器和编辑器的麻烦. 我们可以使用Element面板来查看源代码,它不 ...

  4. [转]理解RESTful架构

    原文地址:http://www.ruanyifeng.com/blog/2011/09/restful 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件" ...

  5. 100803C

    画个图,很容易发现少兜圈子,就是说这些限制c[i],d[i]可以看做[c[i],d[i]],不让那些区间相交,然后就可以了 #include<iostream> #include<c ...

  6. eclipse-mvn打包跳过junit测试类

    修改pom.xml,在build选项加上plugins的这段如下: <build> ..... <plugins> <plugin> <groupId> ...

  7. Activity的四种launchMode

    来源:http://blog.csdn.net/liuhe688/article/details/6754323/ 我们今天要讲的是Activity的四种launchMode. launchMode在 ...

  8. 【CCCC天梯赛决赛】

    cccc的天梯赛决赛,水题一样的水,中档题以上的还是没做出来.补了一下题,觉得其实也不是很难,主要是练的少. L2-1:红色预警 并查集 我做的时候想不到并查集,想到了也不一定做的出来,都是其实不难. ...

  9. bzoj4403: 序列统计

    我们很容易发现答案是C(R-L+N+1,N)-1 然后用一下lucas定理就行了 #include <iostream> #include <cstdio> #include ...

  10. BZOJ 1116: [POI2008]CLO

    1116: [POI2008]CLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 922  Solved: 514[Submit][Status][ ...