---
title: "I/O 3JAVA IO详解（字符流）"
author: "mynote"
date: "2011-08-04T23:15:01Z"
tags: []
categories: ["编程语言"]
slug: "i-o-3java-io"
draft: false
lastmod: "2011-08-05T00:50:22Z"
id: 1140509
url: "https://mynote.iteye.com/blog/1140509"
---

![](http://dl.iteye.com/upload/picture/pic/96044/b8574952-aa31-3528-bf64-afc09264fb62.jpg)

![](http://dl.iteye.com/upload/picture/pic/96046/9d872564-32f7-33e2-8480-c141ec67068e.jpg)

![](http://dl.iteye.com/upload/picture/pic/96048/46717d9b-d4e5-3665-8a2e-12e9c12a6528.jpg)

![](http://dl.iteye.com/upload/picture/pic/96052/502e846f-00e6-3223-880b-1ec5be447428.jpg)

![](http://dl.iteye.com/upload/picture/pic/96054/f91191e6-c9bf-3c42-869c-ece2cb20db89.jpg)

![](http://dl.iteye.com/upload/picture/pic/96056/3be0ab76-42bd-3445-91a3-e6412f9d2876.jpg)

![](http://dl.iteye.com/upload/picture/pic/96058/d853e16c-9d53-3ac0-9135-cb444e60b060.jpg)

![](http://dl.iteye.com/upload/picture/pic/96060/37b8f89c-b306-3383-9501-512c612e221d.jpg)

![](http://dl.iteye.com/upload/picture/pic/96062/eebad98d-2fbe-3e41-bf62-3099c1efa719.jpg)

![](http://dl.iteye.com/upload/picture/pic/96064/2e17893f-1d7d-3a2f-aa3f-2548d3647131.jpg)

![](http://dl.iteye.com/upload/picture/pic/96066/1bf3eee7-9788-3085-a91d-c6ed7d036f9d.jpg)

![](http://dl.iteye.com/upload/picture/pic/96068/c7852b79-ffa3-3f85-820e-eb6c5a1e532d.jpg)

 

StreamTest

```
package com.test.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class StreamTest {
	public static void main(String[] args) throws IOException {
		
		FileOutputStream fos = new FileOutputStream("test.txt");
		OutputStreamWriter osw = new OutputStreamWriter(fos);
		BufferedWriter bw = new BufferedWriter(osw); //装饰模式
		
		bw.write("www.google.com");
		bw.write("\n");
		bw.write("www.baidu.com");
		bw.close();
		
		FileInputStream fis = new FileInputStream("test.txt");
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		String str = br.readLine();
		while(str != null){
			System.out.println(str);
			str = br.readLine();
		}
	}
}
```

 

```
package com.test.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StreamTest2 {
	public static void main(String[] args)throws IOException {
		//System.in表示从标准输入设备上读取输入
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);

		String str = br.readLine();
		while(null != str){
			System.out.println(str);
			str = br.readLine();
		}
		br.close();
	}
}
```

 

![](http://dl.iteye.com/upload/picture/pic/96070/b32de8b6-f7d7-35c4-ab37-4a46e82768b5.jpg)

![](http://dl.iteye.com/upload/picture/pic/96072/89f03d3f-f75a-33b4-a334-ab96eac800e1.jpg)

 

```
package com.test.io;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FildReadTest{
	public static void main(String[] args)throws IOException {
		FileReader fr = new FileReader("test.txt");
		BufferedReader br = new BufferedReader(fr);
		String str = br.readLine();
		while(str != null){
			System.out.println(str);
			str = br.readLine();
		}
		br.close();
	}
}
```

 

![](http://dl.iteye.com/upload/picture/pic/96074/d6464097-5e7c-3439-a067-5e628d3bff80.jpg)

![](http://dl.iteye.com/upload/picture/pic/96076/b0592731-c719-333c-b042-2be7001ab9ce.jpg)

 ![](http://dl.iteye.com/upload/picture/pic/96096/f9bd049f-e069-3d22-87ba-5414c3e942f9.jpg)

![](http://dl.iteye.com/upload/picture/pic/96138/b51d4b2a-5625-3d6a-b2bc-fa63a4ead789.jpg)

![](http://dl.iteye.com/upload/picture/pic/96140/a18acfb0-e5dd-3fc9-936f-4e4e7351c46b.jpg)

 

```
package com.test.io;

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileTest {
	public static void main(String[] args) throws IOException {
		Person person1 = new Person("zhangsan",11,1.68);
		RandomAccessFile raf = new RandomAccessFile("person.txt","rw");
		person1.write(raf);
		
		raf.seek(0);//让读的位置重新回到文件开关
		
		Person person2 = new Person();
		person2.read(raf);
		System.out.println(person2.getName());
		System.out.println(person2.getAge());
		System.out.println(person2.getHeight());
	}
}
class Person{
	private String name;
	private int age;
	private double height;
	
	public Person(){
	}
	
	public Person(String name,int age,double height){
		this.name = name;
		this.age = age;
		this.height = height;
	}
	
	public void write(RandomAccessFile raf) throws IOException{
		raf.writeInt(age);
		raf.writeUTF(name);
		raf.writeDouble(height);
	}
	
	public void read(RandomAccessFile raf)throws IOException{
		this.age = raf.readInt();
		this.name = raf.readUTF();
		this.height = raf.readDouble();
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}
}
```