---
title: " I/O 2:JAVA IO详解（流类）"
author: "mynote"
date: "2011-08-02T01:04:16Z"
tags: []
categories: ["编程语言"]
slug: "i-o-2-java-io"
draft: false
lastmod: "2011-08-04T22:22:53Z"
id: 1137773
url: "https://mynote.iteye.com/blog/1137773"
---

![](http://dl.iteye.com/upload/picture/pic/95770/d00a9786-572f-35b5-a219-18f2f81f05d3.jpg)

![](http://dl.iteye.com/upload/picture/pic/95772/bdd92694-01e5-39c5-b6f5-9d2e8d076f31.jpg)

![](http://dl.iteye.com/upload/picture/pic/95774/14792c3a-17fb-3712-a285-a804711e9995.jpg)

![](http://dl.iteye.com/upload/picture/pic/95776/17669df7-5ba0-37f4-a6d1-19a5d3254e21.jpg)

![](http://dl.iteye.com/upload/picture/pic/95778/30c924eb-132f-3bb8-b3eb-26654ee302a6.jpg)

![](http://dl.iteye.com/upload/picture/pic/95780/4b33fba6-61bc-3f64-8a72-771b84498f7e.jpg)

![](http://dl.iteye.com/upload/picture/pic/95782/18f757a8-be01-3896-b09c-d9bbbc413cd0.jpg)

![](http://dl.iteye.com/upload/picture/pic/95784/38787f7c-b78e-3683-a671-7f1033ae9d24.jpg)

![](http://dl.iteye.com/upload/picture/pic/95786/3ae6cdea-75ce-30bb-b5be-c9027adfec38.jpg)

![](http://dl.iteye.com/upload/picture/pic/95788/b38ec85d-5b33-3bf0-8c88-03e54366525a.jpg)

![](http://dl.iteye.com/upload/picture/pic/95790/665ba307-960c-328c-bc44-0d3e546da10b.jpg)

![](http://dl.iteye.com/upload/picture/pic/95792/cb07c6b4-aecd-30ef-b8b6-fb3b75283287.jpg)

![](http://dl.iteye.com/upload/picture/pic/95794/f774267f-e84c-396a-b933-d2fa1bb14ef5.jpg)f

 

```
package com.test.Algorithm;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileTest10 {
	//上面提到读数据的步骤为：1，打开一个流。2，判断是否还有数据，并读取数据。3，关闭流。
	public static void main(String[] args) throws IOException {
		//打开流
		FileInputStream fis = new FileInputStream(new File("C:/优秀.txt"));
		byte[] buffer = new byte[200];
		int length = 0;
		//判断是否有更多数据，当没有时，返回-1。
		//上面定义了一个200长度的字节数据，如果test.txt中有500字节长度的数据。
		//那么它会循环4次，lenght的长度分别是200,200,100,-1
		while (-1 != (length = fis.read(buffer, 0, 200))) {
			String s = new String(buffer, 0, length);
			System.out.println(s);
		}
		fis.close();
	}

}
```

 

![](http://dl.iteye.com/upload/picture/pic/95872/46520541-5860-3152-becd-5d9dfda37406.jpg)

InputStream子类中FilterInputStream是过滤流，其它是节点流。

![](http://dl.iteye.com/upload/picture/pic/95878/a63ffe6a-438c-30d2-96cf-56dd2dff5216.jpg)

![](http://dl.iteye.com/upload/picture/pic/95880/80234d8b-c7da-3949-83e6-af0247ce9797.jpg)

![](http://dl.iteye.com/upload/picture/pic/95882/982574cb-cc14-37b5-8615-7e83b20902c2.jpg)

 

以下程序简单演示了FileOutputStream的用法

```
package com.test.Algorithm;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileTest9 {
	public static void main(String[] args) throws IOException {
		FileOutputStream os = new FileOutputStream("d:/test.txt");
		String str = "hello world";
		byte[] buffer = str.getBytes();
		os.write(buffer);
		os.close();
	}
}
```

其中FileOutputStream有一个构造函数

```
FileOutputStream(File file, boolean append) 
```

参数append表示内容是否追加  
![](http://dl.iteye.com/upload/picture/pic/95884/b62abd79-3051-389a-be29-4928d44334fc.jpg)

![](http://dl.iteye.com/upload/picture/pic/95886/add0df35-afb1-3a1e-9989-8a42172877b6.jpg)

![](http://dl.iteye.com/upload/picture/pic/95888/3d6b9ef0-4b75-3356-925a-1d2bd7219d96.jpg)

![](http://dl.iteye.com/upload/picture/pic/95890/14a30441-931d-3c48-85dd-aa71c0128d1c.jpg)

![](http://dl.iteye.com/upload/picture/pic/95892/e4bb50a6-93c5-3a90-9544-88a60165b69b.jpg)

 

```
package com.test.Algorithm;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FileTest9 {
	public static void main(String[] args) throws IOException {
		ByteArrayOutputStream f = new ByteArrayOutputStream();
		String str = "hello world";
		byte[] buffer = str.getBytes();
		f.write(buffer);
		byte[] result = f.toByteArray();
		for (int i = 0; i < result.length; i++) {
			System.out.println((char) result[i]);
		}
		OutputStream os = new FileOutputStream("text.txt");
		f.writeTo(os);
		f.close();
		os.close();
	}
}
```

 

![](http://dl.iteye.com/upload/picture/pic/95970/cf2fdec7-ac3c-3886-9471-1e50061cfc02.jpg) 

 

```
package com.test.Algorithm;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FileTest9 {
	public static void main(String[] args) throws IOException {
		DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(
				new FileOutputStream("a.txt")));
		byte b = 3;
		int i =12;
		char ch ='a';
		float f = 3.3f;
		dos.writeByte(b);
		dos.writeInt(i);
		dos.writeChar(ch);
		dos.writeFloat(f);
		dos.close();
		
		DataInputStream dis = new DataInputStream(new BufferedInputStream(
				new FileInputStream("a.txt")));
		//读与写的顺序要保持一致
		System.out.println(dis.readByte());
		System.out.println(dis.readInt());
		System.out.println(dis.readChar());
		System.out.println(dis.readFloat());
		dis.close();
	}
}
```

 

![](http://dl.iteye.com/upload/picture/pic/95972/efac37b0-6620-352a-9bea-cbf5374a25cf.jpg)

![](http://dl.iteye.com/upload/picture/pic/95974/e6cd1745-a83c-3e9f-b452-1127f1b52bd0.jpg)

![](http://dl.iteye.com/upload/picture/pic/95976/656c72bc-c0eb-3cee-8b7f-e1af27468fb2.jpg)

![](http://dl.iteye.com/upload/picture/pic/95978/036632f3-6d1e-34e9-8711-7e41f4e8e411.jpg)

![](http://dl.iteye.com/upload/picture/pic/95982/b2dfd75d-6a1a-317f-bcdd-79c3a24f5296.jpg)

![](http://dl.iteye.com/upload/picture/pic/95984/23029811-de08-3c50-8728-e48f9b0a557a.jpg)

![](http://dl.iteye.com/upload/picture/pic/95986/27d55a90-60d1-3040-b363-c34f3270985c.jpg)

![](http://dl.iteye.com/upload/picture/pic/95988/2605489f-3646-3230-aeba-707cd46e46b7.jpg)

```
package com.test.Algorithm;

import java.io.IOException;
import java.io.InputStream;

public class MyByteArrayInputStream extends InputStream{
	
	protected byte[] data;
	protected int position = 0;
	
	public MyByteArrayInputStream(byte[] b){
		this.data = b;
	}
	
	public int read() throws IOException {
		return (position < data.length) ? (data[position++]) : -1;
	}
	
	public static void main(String[] args) {
		byte[] b = new byte[16];
		for (int i = 0; i < b.length; i++) {
			b[i] = (byte)i;
		}
		MyByteArrayInputStream mbais = new MyByteArrayInputStream(b);
		while(true){
			try {
				int c = mbais.read();
				if(c < 0){
					break;
				}
				System.out.println(c + "  ");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
```

 

![](http://dl.iteye.com/upload/picture/pic/95990/fa6e0181-b2ec-353a-9f93-a20c39dd3311.jpg)

![](http://dl.iteye.com/upload/picture/pic/95992/363ef95b-f710-351d-9d85-740ecff2a9ce.jpg)

 

```
package com.test.Algorithm;

import java.io.IOException;
import java.io.InputStream;

public class MyOwnStream extends InputStream{
	protected byte[] data;
	protected int position = 0;
	protected int mark =0;
	public MyOwnStream(byte[] b){
		this.data = b;
	}
	public int read() throws IOException {
		return (position < data.length) ? (data[position++]) : -1;
	}
	//返回仍可读取的字节长度
	public int available() throws IOException {
		return data.length - position;
	}
	
	public void close() throws IOException {
		position = data.length;
	}
	//设置流中的标识位
	public synchronized void mark(int readlimit) {
		this.mark = readlimit;
	}
	//将缓冲流复位到标识位置
	public synchronized void reset() throws IOException {
		if(mark <0 || mark >= data.length){
			throw new IOException("the position is not valid");
		}
		position = mark;
	}
	
	public boolean markSupported() {
		return true;
	}
	
	public int read(byte[] b, int off, int len) throws IOException {
		if(this.position > data.length || len < 0){
			return -1;
		}
		if((this.position + len) > data.length){
			len = data.length - this.position;
		}
		//如果只读0个，就直接返回0
		if(len == 0){
			return 0;
		}
		System.arraycopy(data, position, b, off, len);
		position +=len;
		return len;
	}
	
}
```

 

学IO最重要的是深刻理解装饰模式！