文档章节

FileChannelMain

ruyees
 ruyees
发布于 2014/10/05 11:47
字数 404
阅读 3
收藏 0
点赞 0
评论 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
深圳
产品经理

暂无相关文章

Qt中的坑--QTreeWidget添加item 不能显示出来

QTreeWidget* pTree = ui.TreeCheckList; QTreeWidgetItem* item = new QTreeWidgetItem(pTree) ;item->setText ( 0, "test" );pTree->addTopLevelItem (item ); 原因是因为创建一个......

k91191 ⋅ 33分钟前 ⋅ 0

使用Guava的RateLimiter做限流

场景: 1. 在日常生活中,我们肯定收到过不少不少这样的短信,“京东最新优惠卷…”,“天猫送您…”。这种类型的短信是属于推广性质的短信。这种短信一般群发量会到千万级别。然而,要完成这...

wind2012 ⋅ 33分钟前 ⋅ 0

QSlider重新enterEvent

#ifndef DIALOG_H#define DIALOG_H#include <QDialog>namespace Ui {class Dialog;}class Dialog : public QDialog{ Q_OBJECTpublic: explicit Dialog(QW......

xxdd ⋅ 34分钟前 ⋅ 0

生产环境redis备份与恢复

生产环境redis备份与恢复 Tyrant0532 0人评论 1563人阅读 2018-02-01 20:34:10 redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。生产中我们主...

rootliu ⋅ 36分钟前 ⋅ 0

nginx中出现403forbidden错误

nginx “403 Forbidden” 错误 出现这个错误一般是因为以下原因: 网站禁止特定的用户访问所有内容,例:网站屏蔽某个ip访问。 访问禁止目录浏览的目录,例:设置autoindex off后访问目录。 ...

河图再现 ⋅ 36分钟前 ⋅ 0

上海云栖:金融政企行业的CDN最佳实践

摘要: 在刚刚结束的上海云栖大会飞天技术汇分论坛上,阿里云视频云产品架构师罗小飞进行了《阿里云CDN——面向金融政企的CDN最佳实践》主题分享,为上海的嘉宾介绍CDN的解决方案与技术服务体...

猫耳m ⋅ 42分钟前 ⋅ 0

docker 基本操作

docker介绍 Docker项目提供了构建在Linux内核功能之上,协同在一起的的高级工具。其目标是帮助开发和运维人员更容易地跨系统跨主机交付应用程序和他们的依赖。Docker通过Docker容器,一个安全...

haoyuehong ⋅ 43分钟前 ⋅ 0

上海云栖:金融政企行业的CDN最佳实践

摘要: 在刚刚结束的上海云栖大会飞天技术汇分论坛上,阿里云视频云产品架构师罗小飞进行了《阿里云CDN——面向金融政企的CDN最佳实践》主题分享,为上海的嘉宾介绍CDN的解决方案与技术服务体...

阿里云云栖社区 ⋅ 45分钟前 ⋅ 0

安装与配置hadoop

一、CentOS7安装 java8,参考centos7.0 安装java1.8,tomcat 二、安装hadoop 版本V3.03 1、下载并解压hadoop # mkdir /usr/local/app# mkdir /usr/local/app/hadoop# cd /usr/local/app/had......

iturtle ⋅ 46分钟前 ⋅ 0

Idea设置Serializable自动生成

File --> Settings --> Editor --> Inspections ->Serialization issues,在该项下找到“Serializable class without 'serialVersionUID' ”并勾选...

Gmupload ⋅ 50分钟前 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部