Java——新IO 通道
- 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 通道的更多相关文章
- Java 新IO
NIO提供全新的底层I/O模型.与最初的java.io包中面向流(stream-oriented)概念不同,NIO采用了面向块的概念(block-oriented).在尽可能的情况下,I/O的操 ...
- Java -- 新IO -- 目录
20.1 Java 新IO简介 20.2 缓冲区与Buffer 例:演示缓冲区的操作流程 Class : IntBufferDemo01 20.2.2 深入缓冲区操作 20.2.3 创建子缓冲区 20 ...
- 怎样获取java新IO的Path文件大小
import org.junit.Test; import java.io.IOException; import java.nio.file.Files; import java.nio.file. ...
- Java——新IO 缓冲区与Buffer
缓冲区和Buffer import java.nio.IntBuffer; //================================================= // File Na ...
- JAVA(六)数据库/网络编程/新IO
成鹏致远 | lcw.cnblog.com |2014-02-05 数据库 1.JDBC概述 JDBC(Java Database Connectivity,Java数据库连接)提供了一种与平台无关的 ...
- java 21 - 15 新IO流 NIO
1:JDK4 新IO要了解的类 Buffer(缓冲),Channer(通道) 2:JDK7 要了解的新IO类 Path:与平台无关的路径. Paths:包含了返回Path的静态方法. public ...
- Java 8特性尝鲜:新新IO
Java 8特性尝鲜:新新IO 在这个专题前面的文章中,我们已经看到,使用Java8的lambda表达式对现有的JDK1.2 I/O库的提升,主要是可以使用lambda表达式来构造java.io.Fi ...
- Java NIO之通道
一.前言 前面学习了缓冲区的相关知识点,接下来学习通道. 二.通道 2.1 层次结构图 对于通道的类层次结构如下图所示. 其中,Channel是所有类的父类,其定义了通道的基本操作.从 Channel ...
- 【NIO】Java NIO之通道
一.前言 前面学习了缓冲区的相关知识点,接下来学习通道. 二.通道 2.1 层次结构图 对于通道的类层次结构如下图所示. 其中,Channel是所有类的父类,其定义了通道的基本操作.从 Channel ...
随机推荐
- PHP之function_handling 函数
function_handling 函数 function_handling 函数 需求: 编写一个函数,传入的参数个数不确定,请求出其和. 使用到 以下几个函数: 代码说明: 函数的参数可以是另外一 ...
- Mybatis 异常: The content of elements must consist of well-formed character data or markup
原因很简单:在ibatis的配置文件中不能出现小于号(>) <delete id="deleteByPrimaryKey" parameterType=&quo ...
- Chrome开发工具Elements面板(编辑DOM和CSS样式)详解
Element 译为“元素”,Element 面板可以让我们动态查看和编辑DOM节点和CSS样式表,并且立即生效,避免了频繁切换浏览器和编辑器的麻烦. 我们可以使用Element面板来查看源代码,它不 ...
- [转]理解RESTful架构
原文地址:http://www.ruanyifeng.com/blog/2011/09/restful 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件" ...
- 100803C
画个图,很容易发现少兜圈子,就是说这些限制c[i],d[i]可以看做[c[i],d[i]],不让那些区间相交,然后就可以了 #include<iostream> #include<c ...
- eclipse-mvn打包跳过junit测试类
修改pom.xml,在build选项加上plugins的这段如下: <build> ..... <plugins> <plugin> <groupId> ...
- Activity的四种launchMode
来源:http://blog.csdn.net/liuhe688/article/details/6754323/ 我们今天要讲的是Activity的四种launchMode. launchMode在 ...
- 【CCCC天梯赛决赛】
cccc的天梯赛决赛,水题一样的水,中档题以上的还是没做出来.补了一下题,觉得其实也不是很难,主要是练的少. L2-1:红色预警 并查集 我做的时候想不到并查集,想到了也不一定做的出来,都是其实不难. ...
- bzoj4403: 序列统计
我们很容易发现答案是C(R-L+N+1,N)-1 然后用一下lucas定理就行了 #include <iostream> #include <cstdio> #include ...
- BZOJ 1116: [POI2008]CLO
1116: [POI2008]CLO Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 922 Solved: 514[Submit][Status][ ...