文档章节

FileChannelMain

ruyees
 ruyees
发布于 2014/10/05 11:47
字数 404
阅读 3
收藏 0

package scan;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

public class FileChannelMain {

	private static final Charset charset = Charset.forName("GBK");

	private static final int BUFFER_CAPACITY = 1024;

	public static void main(String[] args) throws IOException,
			InterruptedException {
		final String srcfilePath = "C:/Users/Burn/Desktop/abc.txt";
		readFile(srcfilePath);

		final String writeFilePath = "C:/Users/Burn/Desktop/abc.txt";
		final String[] lines = new String[] { "line1xxssss", "中文测试",
				"!@#$%^&*()" };
		writeFile(writeFilePath, lines, Boolean.TRUE);
		readFile(writeFilePath);

		final String targetFilePath = "C:/Users/Burn/Desktop/abc.txt";
		copyFile1(srcfilePath, targetFilePath);
		copyFile2(srcfilePath, targetFilePath);
	}

	/**
	 * 
	 * <br>
	 * ------------------------------<br>
	 * 
	 * @param srcfilePath
	 * @param targetPath
	 * @throws IOException
	 */
	private static void copyFile2(String srcfilePath, String targetPath)
			throws IOException {
		File file = new File(targetPath);
		if (!file.getParentFile().exists()) {
			file.mkdirs();
		}
		FileInputStream fileInputStream = new FileInputStream(srcfilePath);
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		FileChannel inChannel = fileInputStream.getChannel();
		FileChannel outChannel = fileOutputStream.getChannel();
		// 两者等价
		// inChannel.transferTo(0, inChannel.size(), outChannel);
		outChannel.transferFrom(inChannel, 0, inChannel.size());

		close(fileOutputStream);
		close(fileInputStream);
		close(inChannel);
		close(outChannel);
	}

	/**
	 * 
	 * <br>
	 * ------------------------------<br>
	 * 
	 * @param srcfilePath
	 * @param targetPath
	 * @throws IOException
	 */
	private static void copyFile1(String srcfilePath, String targetPath)
			throws IOException {
		File file = new File(targetPath);
		if (!file.getParentFile().exists()) {
			file.mkdirs();
		}
		FileInputStream fileInputStream = new FileInputStream(srcfilePath);
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		FileChannel inChannel = fileInputStream.getChannel();
		FileChannel outChannel = fileOutputStream.getChannel();
		ByteBuffer inBuffer = ByteBuffer.allocate(BUFFER_CAPACITY);
		while (inChannel.read(inBuffer) != -1) {
			inBuffer.flip();
			outChannel.write(inBuffer);
			inBuffer.clear();
		}
		close(fileOutputStream);
		close(fileInputStream);
		close(inChannel);
		close(outChannel);
	}

	/**
	 * <br>
	 * ------------------------------<br>
	 * 
	 * @param writeFilePath
	 * @param lines
	 * @param append
	 * @throws IOException
	 */
	private static void writeFile(String writeFilePath, String[] lines,
			boolean append) throws IOException {
		File file = new File(writeFilePath);
		if (!file.getParentFile().exists()) {
			file.mkdirs();
		}
		FileOutputStream fileOutputStream = new FileOutputStream(file, append);
		FileChannel fileChannel = fileOutputStream.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(BUFFER_CAPACITY);
		for (String line : lines) {
			buffer.put(line.getBytes());
			buffer.put("\r\n".getBytes());
			buffer.flip();
			fileChannel.write(buffer);
			buffer.clear();
		}
		close(fileOutputStream);
		close(fileChannel);
	}

	/**
	 * <br>
	 * ------------------------------<br>
	 * 
	 * @param path
	 * @throws IOException
	 */
	private static void readFile(String path) throws IOException {
		if (isFileNotExists(path)) {
			throw new FileNotFoundException();
		}
		FileInputStream fileInputStream = new FileInputStream(path);
		FileChannel fileChanne = fileInputStream.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(BUFFER_CAPACITY);
		while (fileChanne.read(buffer) != -1) {
			buffer.flip();
			System.out.println(charset.decode(buffer));
			buffer.clear();
		}
		close(fileInputStream);
		close(fileChanne);
	}

	private static boolean isFileNotExists(String path) {
		File file = new File(path);
		return !file.exists();
	}

	/**
	 * 
	 * <br>
	 * ------------------------------<br>
	 * 
	 * @param outputStream
	 */
	private static void close(OutputStream outputStream) {
		if (outputStream == null)
			return;
		try {
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * <br>
	 * ------------------------------<br>
	 * 
	 * @param channel
	 */
	private static void close(Channel channel) {
		if (channel == null)
			return;
		try {
			channel.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * <br>
	 * ------------------------------<br>
	 * 
	 * @param inputStream
	 */
	private static void close(InputStream inputStream) {
		if (inputStream == null)
			return;
		try {
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 

本文转载自:http://zuoqiang.iteye.com/blog/1698780

共有 人打赏支持
ruyees
粉丝 3
博文 71
码字总数 0
作品 0
深圳
产品经理
私信 提问

暂无文章

GraphQL 基金会成立了:将托管于Linux 基金会管理

GraphQL 基金会在11月7日宣布成立,并将由 Linux 基金会管理。GraphQL 基金会将专用于完善和稳定 GraphQL 生态系统,让 GraphQL 得到广泛关注和采用。GraphQL 是 Facebook 开发的一个应用层数...

Linux就该这么学
16分钟前
5
0
Java开发中内存模型详细解析

Java内存模型虽说是一个老生常谈的问题 ,也是大厂面试中绕不过的,甚至初级面试也会问到。但是真正要理解起来,还是相当困难,主要这个东西看不见,摸不着。网上已经有大量的博客,但是人家...

金铭鼎IT教育
19分钟前
3
0
render常用模版 结合iview

表格添加一对按钮 { title: '操作', align: 'center', render: (h, params) => { return h('div', [ h('Button', { props: { ......

Carbenson
36分钟前
1
0
一次生产 CPU 100% 排查优化实践

前言 到了年底果然都不太平,最近又收到了运维报警:表示有些服务器负载非常高,让我们定位问题。 还真是想什么来什么,前些天还故意把某些服务器的负载提高(没错,老板让我写个 BUG!),不...

crossoverJie
43分钟前
8
0
Spring Cloud Alibaba Sentinel 整合 Feign 的设计实现

作者 | Spring Cloud Alibaba 高级开发工程师洛夜 来自公众号阿里巴巴中间件投稿 前段时间 Hystrix 宣布不再维护之后(Hystrix 停止开发。。。Spring Cloud 何去何从?),Feign 作为一个跟 ...

Java技术栈
59分钟前
9
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部