FileChannelMain

24 篇文章 0 订阅
20 篇文章 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();
		}
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值