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 ...
随机推荐
- 创建Maven项目
在MyEclipse10中创建Maven Web项目 1.构建maven项目 2.将maven项目转换成Dynamic Web Project 3.设置部署集 4.pom.xml文件配置 参考: ht ...
- nginx启动、重启、关闭
一.启动 cd usr/local/nginx/sbin ./nginx 二.重启 更改配置重启nginx kill -HUP 主进程号或进程号文件路径 或者使用 cd /usr/local/ngin ...
- [转]为什么我要用 Node.js? 案例逐一介绍
原文地址:http://blog.jobbole.com/53736/ 介绍 JavaScript 高涨的人气带来了很多变化,以至于如今使用其进行网络开发的形式也变得截然不同了.就如同在浏览器中一样, ...
- hive中行转换成列
python代码 #!/bin/bashimport sys; if __name__=='__main__': for line in sys.stdin: m=line.strip().split ...
- UIScrollView 实践经验(转)
转载自:http://tech.glowing.com/cn/practice-in-uiscrollview/ UIScrollView(包括它的子类 UITableView 和 UICollect ...
- The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 51716619E084DAB9
sudo su gpg --keyserver keyserver.ubuntu.com --recv-keys 51716619E084DAB9 gpg -a --export 51716619E0 ...
- Java中Unicode的编码和实现
Unicode的编码和实现 大概来说,Unicode编码系统可分为编码方式和实现方式两个层次. 编码方式 字符是抽象的最小文本单位.它没有固定的形状(可能是一个字形),而且没有值.“A”是一个字符,“ ...
- Leetcode 347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- eclipse中xml文件不能自动提示的解决办法
1.出现不能提示的情况
- 【bzoj1408】 Noi2002—Robot
http://www.lydsy.com/JudgeOnline/problem.php?id=1408 (题目链接) 题意 定义了3种数,分别求这3种数的φ的和,其中φ(1)=0. Solution ...