---
title: "缓冲流"
author: "show213"
date: "2013-06-20T18:08:33Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-06-20T18:08:33Z"
id: 1890796
url: "https://show213.iteye.com/blog/1890796"
---

关于IO流，先写一个小程序，这个程序主要是将存控制台的输入保存到硬盘的文件中。

```java

public void FromScreen01(String path){

		try {
			System.out.print("请输入：");
//从控制台的输入源输入到缓冲器			
InputStreamReader isr = new InputStreamReader(System.in);			//从缓冲器输出到文件
			FileOutputStream fi=new FileOutputStream(path);		
			int number=0;
			while((number = isr.read())!= 13){
				fi.write(number);
			}			
			System.out.println("输入成功！");			
		} catch (Exception e) {
			e.printStackTrace();
		}		
		
	}

```

在这个程序中，对于读的方法，我只写了一个InputStream流，按角色来分这个流属于节点流，也就是低级流。



```java

public void FromScreen02(String path){

		try {
			
	System.out.print("请输入：");
			InputStreamReader isr = new InputStreamReader(System.in);			
			BufferedReader br = new BufferedReader(isr);			
			FileOutputStream fi=new FileOutputStream(path);			
			
			String str=br.readLine();			
			
			fi.write(str.getBytes());
			System.out.println("输入成功！");			
		} catch (Exception e) {
			e.printStackTrace();
		}		
		
	}


```

这个方法里我只是多加入了一个BufferedReader 缓冲流，按角色划分为处理流，也就是高级流。


```java


public void FromScreen03(String path){
		try {
			
			System.out.print("请输入：");
			BufferedInputStream bis = new BufferedInputStream(System.in);			
			FileOutputStream fi=new FileOutputStream(path);			
			int number=0;
			while((number = bis.read())!= 13){
				fi.write(number);
			}			
		
			System.out.println("输入成功！");			
		} catch (Exception e) {
			e.printStackTrace();
		}		
		
	}


```

这个方法里我也只是多加入了一个BufferedInputStream缓冲流，按角色划分为处理流，也就是高级流。

那看完这三个方法后总结一下：
第一个方法没有高级流做封闭，如果遇到中文问题，就会显示乱码。２和３则不会。
２跟３比，２的效率可能要高一些。