文档章节

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
深圳
产品经理

暂无文章

React 服务器渲染原理解析与实践

网盘下载地址 React 服务器渲染原理解析与实践 本套课程,讲解了React中SSR技术的整个搭建思路及流程,完整的从原理上讲清楚了SSR的概念,重点在于讲解编写SSR框架遇到的各种知识点,以及细节...

qq__2304636824
40分钟前
0
0
Jenkins使用

clean install -Dmaven.test.skip=true

1713716445
50分钟前
0
0
多线程

1. 多线程概念。并发和并行的概念。 多线程指的是一段时间内cpu同时执行多个线程。一个程序至少运行>=1个进程,进程就是运行中的程序,而一个进程至少运行>=1个线程,线程是操作系统能调度的...

鱼想吃肉
今天
1
0
HBase 表修复在线方式和离线方式

一、在线修复 1.1 使用检查命令 $ ./bin/hbase hbck 该命令可完整修复 HBase 元数据信息;存在有错误信息会进行输出; 也可以通过如下命令查看详细信息: $ ./bin/hbase hbck -details 1.2 ...

Ryan-瑞恩
今天
3
0
redis 系列二 -- 常用命令

1.基础命令 info ping quit save dbsize select flushdb flushall 2.键命令 2.1 set 直接赋值 set a a 2.2 get 取值 get a 2.3 exists 是否存在 exists a 2.4 expire 设置剩余时间 秒 expire......

imbiao
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部