IO読み書きファイルの簡単な性能比較


public class InputStreamTest {

	public static void main(String[] args) {
		InputStreamTest inputstream = new InputStreamTest();
		FileInputStreamExample example = inputstream.new FileInputStreamExample("D:\\istream\\rechargeLarge.txt", "D:\\istream\\rechargeCopy.txt");
		long start = System.currentTimeMillis();
//		example.copy();
		example.copyReader();
//		example.copyNio();
		System.out.println(System.currentTimeMillis() - start);
	}
	
	public class FileInputStreamExample {
		
		String inputPath;
		String outputPath;
		
		public FileInputStreamExample(String inputPath, String outputPath) {
			this.inputPath = inputPath;
			this.outputPath = outputPath;
		}
		
		public FileInputStreamExample(String inputPath) {
			this(inputPath, null);
		}
		
		public void copy() {
			FileInputStream inputStream = null;
			BufferedInputStream bufferInputStream = null;
			FileOutputStream outputStream = null;
			BufferedOutputStream bufferOutputStream = null;
			try {
				inputStream = new FileInputStream(inputPath);
				bufferInputStream = new BufferedInputStream(inputStream);
				outputStream = new FileOutputStream(outputPath);
				bufferOutputStream = new BufferedOutputStream(outputStream);
				
				int n = 0;
				byte[] but = new byte[1024];
				while((n = bufferInputStream.read(but)) != -1) {
					// 
					bufferOutputStream.write(but, 0, n);
				}
				bufferOutputStream.flush();
				
//				System.out.println(" " + inputStream.read());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					bufferInputStream.close();
					inputStream.close();
					bufferOutputStream.close();
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		public void copyReader() {
			FileReader fileReader = null;
			BufferedReader bufferedReader = null;
			FileWriter fileWriter = null;
			BufferedWriter bufferedWriter = null;
			try {
				fileReader = new FileReader(inputPath);
				bufferedReader = new BufferedReader(fileReader);
				fileWriter = new FileWriter(outputPath);
				bufferedWriter = new BufferedWriter(fileWriter);
				
				int n = 0;
				char[] buf = new char[1024];
				while ((n = bufferedReader.read(buf)) != -1) {
					bufferedWriter.write(buf, 0, n);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					bufferedReader.close();
					fileReader.close();
					bufferedWriter.close();
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		public void copyNio() {
			FileInputStream inputStream = null;
			FileOutputStream outputStream = null;
			FileChannel inputChannel = null;
			FileChannel outputChannel = null;
			try {
				inputStream = new FileInputStream(inputPath);
				outputStream = new FileOutputStream(outputPath);
				inputChannel = inputStream.getChannel();
				outputChannel = outputStream.getChannel();
				
				ByteBuffer buffer = ByteBuffer.allocate(1024);
				while(inputChannel.read(buffer) != -1) {
					buffer.flip();
					outputChannel.write(buffer);
					buffer.clear();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					inputStream.close();
					outputStream.close();
					inputChannel.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

 
Windows 7プラットフォームでは、3つのIO例の簡単な性能比較を行いました.比較により,ファイルIOではNIOはストリームよりも速くなく,バッファサイズの影響を受け,同じバッファサイズの条件下ではバイトによる読み書きストリームの速度がNIOよりもむしろ速いことが分かった.文字ストリームの場合、バイトストリームに基づいて構築されるため、符号化情報を処理する必要があり、その読み書き速度はバイトストリームよりも遅い.