FileChannelMain
博客专区 > ruyees 的博客 > 博客详情
FileChannelMain
ruyees 发表于3年前
FileChannelMain
  • 发表于 3年前
  • 阅读 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();
		}
	}
}
 
共有 人打赏支持
粉丝 3
博文 71
码字总数 0
×
ruyees
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: