利用FileChannel完成文件的读、写、复制

内容:通过NIO中的FileChannel完成文件的读、写、复制。

public class NioFileCopy {
	private RandomAccessFile aFile = null;
	private FileChannel inChannel = null;
	private final ByteBuffer buf = ByteBuffer.allocate(1024);

	public void doWrite() throws IOException {
		aFile = new RandomAccessFile("C:/goods.txt", "rw");
		inChannel = aFile.getChannel();
		String newData = "New String to wirte to file... " + System.currentTimeMillis();
		buf.clear();
		buf.put(newData.getBytes());

		buf.flip();

		while (buf.hasRemaining())
			inChannel.write(buf);

		inChannel.close();
		System.out.println("Write Over");
	}

	public void doRead() throws IOException {
		aFile = new RandomAccessFile("C:/goods.txt", "rw");
		inChannel = aFile.getChannel();

		int bytesRead = inChannel.read(buf);
		while (bytesRead != -1) {
			System.out.println("Read " + bytesRead);
			buf.flip();
			while (buf.hasRemaining())
				System.out.print((char) buf.get());

			buf.clear();
			bytesRead = inChannel.read(buf);
		}

		aFile.close();
	}

	public void doCopy() throws IOException {
		aFile = new RandomAccessFile("C:/goods.txt", "rw");
		inChannel = aFile.getChannel();
		RandomAccessFile bFile = new RandomAccessFile("C:/22.log", "rw");
		FileChannel outChannel = bFile.getChannel();
		inChannel.transferTo(0, inChannel.size(), outChannel);
		System.out.println("Copy over");
	}

	public static void main(String[] args) throws IOException {
		NioFileCopy tool = new NioFileCopy();
		//tool.doWrite();
		//tool.doRead();
		tool.doCopy();
	}
}
时间: 2024-08-03 22:32:50

利用FileChannel完成文件的读、写、复制的相关文章

asp.net 文件操作小例子(创建文件夹,读,写,删)

静态生成要在虚拟目录下创建文件夹 来保存生成的页面 那么就要对文件进行操作 一.创建文件夹 using System.IO; string name = "aa"; string path = Server.MapPath("") + "\\" + name; if (Directory.Exists(path)) { Response.Write("<script>alert('文件夹已存在了!');history.go(

Python 文件(读\写)操作

文件(读\写)操作 open()函数,用来打开文件,创建file对象.open(name[,mode[,buffering]])name:要打开的文件mode:是打开文件的模式(读.写.追加)buffering:是否要寄存,默认为0或者False(不寄存),1或True表示寄存(意味着使用内存来代替硬盘,让程序更快,只有使用flush或者close才会更新硬盘上的数据),如果大于1表示寄存区的缓冲大小(单位:字节),如果是负值,表示寄存区为系统默认大小. open函数中模式参数的常用量:r 读模

黑马程序员——File笔记读,写,复制

#region ReadAllBytes byte[] buffer = File.ReadAllBytes(@"C:\Users\dell\Desktop\新建文件夹.txt"); //将字节数组转换成字符串//解决乱码 GB2312 GBK Default UTF8//string str = System.Text.Encoding.UTF8.GetString(buffer);//Console.WriteLine(str);//Console.ReadKey(); #endr

测试利用多线程进行文件的写操作

最近学习NIO技术,了解了有关channel通道.buffer缓存以及selector选择器等技术,萌发了想写一个多点下载的一个简单测试demo.我将这个demo分成两步,第一步先实现将一个文件分段复制到一个文件中(通常我们是将文件以流的形式一个字节一个字节的复制到目标文件中,现在我们是将文件分段,启用多个线程,每个线程复制一部分,然后再根据原文件分段的位置组装成一个文件,实现高效的目的).下面帖源码 /** * 测试利用多线程进行文件的写操作 * @author gcl * @version

java并发读&写文件

最近在看Brian Goetz 的<<Java并发实战>>,这本书有两个版本,电子工业出版社的译本很糟糕,建议使用机械工业出版社出版出版的书籍. 在看到第三四章的时候突然想到了多线程读写文件,同时遇到一些书中没有的问题 1, 如何保证组合对象的安全性? 2, 如何判断不变性的约束条件 3, 如何不通过synchronized关键字和锁进行同步处理? 下面是一段代码, 用来从source 读取数据,通过多线程写入target文件中 思路: 1, 如何read/write文件? 2,

利用字节流或字符流实现文件夹整体的复制

思路: 1.获取所有的文件夹和子文件的名称然后遍历 2.判断是否是文件夹,是的话进行复制 eg:对D盘mp3文件夹内的所有文件进行复制,复制到F盘MP3文件夹. (1).判断F盘下是否有mp3文件夹,没有的话进行创建. (2).如果创建其他的字目录,需要获得他的目录名和路径(划重点) 3.判断是不是文件,是的话复制(可以用递归) 发挥大事化小,小事化了的精神,我们分为三个部分: 1.写一个复制子文件的方法: 2.写一个复制文件夹的方法: 3.写一个main函数(废话): eg:分开写是为了方便理

ubuntukylin基础 chmod 对一个文件的所有者,用户组,其他人分别添加或删除 读 写 执行 的权限

镇场文:       学儒家经世致用,行佛家普度众生,修道家全生保真,悟易理象数通变.以科技光耀善法,成就一良心博客.______________________________________________________________________________________________________ 我的系统:UbuntuKylin 16.04 LTS 64bit step0: 查看指定文件的权限 step1: 减去所有者的 读 写 执行的权限 step2: 查看执行效果

02_Android写xml文件和读xml文件

?? 新建Android项目 编写AndroidManifest.xml,使本Android项目具有单元测试功能和写外设的权限. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima28.xmldemo" a

java中文件的读与写

最近一直在学习java中如何读取和写出文件,看了java API之后,发现在java.io中有很多关于文件读与写的类,下面就介绍几个经常用到的. 首先是:InputStream和OutputStream,API中说它俩是所有抽象类表示字节输入输出流的超类,所以在它们下面派生了很多子类.例如:FileInputStream和OutputStream等等.这些类都是以单字节的形式读入数据的,所以如果读入的数据中有中文字符,那么就会出现中文乱码现象. 其次是:Reader和Writer,这两个类是用于