---
title: "NIO"
author: "yangwn"
date: "2010-02-02T23:42:44Z"
tags: []
categories: ["编程语言"]
slug: "nio"
draft: false
lastmod: "2010-02-03T15:03:49Z"
id: 586182
url: "https://yangwn.iteye.com/blog/586182"
---

很久没有使用IO进行处理文件了，虽然很简单，不过自己也写了一个测测性能。

```
package com.tata.java.io;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileDealManager {

	public static void main(String[] args) throws IOException {
		FileDealManager fileManager = new FileDealManager();
		long startTime = System.currentTimeMillis();
		fileManager.readWrite("./src/yangwn.txt", "./src/sendy.txt");
		System.out.println("readWrite:"
				+ (System.currentTimeMillis() - startTime));

		startTime = System.currentTimeMillis();
		fileManager.readWriterArray("./src/yangwn.txt", "./src/sendy.txt");
		System.out.println("readWriteBuffer:"
				+ (System.currentTimeMillis() - startTime));
	}

	/**
	 * 单字节无缓冲处理类
	 * 
	 * @param fileFrom
	 * @param fileTo
	 * @throws IOException
	 */
	public static void readWrite(String fileFrom, String fileTo)
			throws IOException {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(fileFrom);
			out = new FileOutputStream(fileTo);
			while (true) {
				int bytedata = in.read();
				if (bytedata == -1) {
					break;
				}
				out.write(bytedata);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 单字节缓冲处理类
	 * 
	 * @param fileFrom
	 * @param fileTo
	 * @throws IOException
	 */
	public static void readWriteBuffer(String fileFrom, String fileTo)
			throws IOException {

		InputStream in = new FileInputStream(fileFrom);
		InputStream inBuffer = new BufferedInputStream(in);
		OutputStream out = new FileOutputStream(fileTo);

		try {
			while (true) {
				int byteData = inBuffer.read();
				if (byteData == -1) {
					break;
				}
				out.write(byteData);
			}
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 自定义缓冲区,最好为512的倍数
	 * 
	 * @param fileFrom
	 * @param fileTo
	 * @throws IOException
	 */
	public static void readWriterArray(String fileFrom, String fileTo)
			throws IOException {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(fileFrom);
			out = new FileOutputStream(fileTo);
			int avalidlength = in.available();
			byte[] totalBytes = new byte[avalidlength];
			int bytedata = in.read(totalBytes);
			out.write(totalBytes);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
	}

}
```

 

   顺便提一下，使用第三种是将文件内容放到内存中处理，如果是大文件，比如说T级的话，是不行的。

   1.采用默认方式输入输出会导致性能下降。(单字节)

   2.采用系统缓冲区进行输入输出对系统进行提升，但是并不是最优的。(单字节)

   3.采用自定义合理缓冲区是性能最好的，但是处理大文件会导致内存溢出。