NIO - FileChannel

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 = "D:/tomcat-6.0.26/logs/catalina.2012-04-30.log";
		readFile(srcfilePath);
		
		final String writeFilePath = "D:/test.txt";
		final String[] lines = new String[]{"line1xxssss", "中文测试", "!@#$%^&*()"}; 
		writeFile(writeFilePath, lines, Boolean.TRUE);
		readFile(writeFilePath);
		
		final String targetFilePath = "D:/test-copy.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();
		}
	}
}
posted on 2012-05-29 23:51  YangJin  阅读(284)  评论(0编辑  收藏  举报