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 ...
随机推荐
- github page 和 hexo 搭建在线博客
目录: 安装node.js与git 常用git命令 安装hexo 配置hexo hexo发布到github 1.安装node.js和git工具 https://nodejs.org/en/ 直接下载安 ...
- 1116Xlinux初学习之正则表达式和通配符
一.正则表达式: 元字符是用来阐释字符表达式意义的字符,简言之,就是用来描述字符的字符. 正则表达式RE(Regular Expression)是由一串字符和元字符构成的字符串. 正则表达式的主要功能 ...
- 侧滑菜单SlidingMenu
想要使用SlidingMenu 需要下载文件SlidingMenu-master 并导入SlidingMenu-master中的第三方library 如图所示: 修改library里的build.gr ...
- C#反射设置属性值和获取属性值
/// /// 获取类中的属性值 /// /// /// /// public string GetModelValue(string FieldName, object obj) { try { T ...
- cocoaPods 的安装和使用
参考:http://www.360doc.com/content/14/0309/10/11029609_358970353.shtml
- C#-WinForm-打开其他窗体的三种方式-Show()、设置Owner()、ShowDialog()
打开其他窗体的三种方式 Show - 例如登入界面进入主页面,直接将主页面展示出来,两个窗体互不影响 public partial class Form1 : Form { public Form1( ...
- JS 弹窗到右下角
<div id="msg_win" style="display: block; top: 490px; visibility: visible; opacity: ...
- 【CodeForces 699A】Launch of Collider
维护最新的R,遇到L时如果R出现过就更新答案. #include <cstdio> #include <algorithm> using namespace std; int ...
- awk 匹配不是 pattern 的内容
awk '/pattern/ {next} {print $0}' filename
- 简进祥-SVN版本控制方案:多分支并行开发,多环境自动部署
两地同时开发一个产品,目前线上有3个环境:测试环境.预发布环境.生产环境.目前系统部署采用jenkins自动化部署工具 用jenkins部署的方案 jenkins 测试环境:配置了各个分支的svn 地 ...