--------------------------------接上篇--------------------------------
3. 字符流基本操作
字符流输入流与字符输出流的父类具体分类如下:
 

3.1 字节字符转换流(读)——InputStreamReader
3.1.1 InputStreamReader类中的方法
| 变量和类型 | 方法 | 说明 | 
|---|---|---|
| String | getEncoding() | 返回此流使用的字符编码的名称。 | 
| int | read() | 读一个字符。 | 
| int | read(char[] cbuf, int offset, int length) | 将字符读入数组的一部分。 | 
| boolean | ready() | 判断此流是否可以读取。 | 
| 构造方法 | public InputStreamReader(InputStream in) | 创建一个使用默认字符集的InputStreamReader。 | 
| 构造方法 | public InputStreamReader(InputStream in, String charsetName)throws UnsupportedEncodingException | 创建一个使用指定charset的InputStreamReader。 | 
| 构造方法 | public InputStreamReader(InputStream in,Charset cs) | 创建一个使用给定charset的InputStreamReader。 | 
| 构造方法 | public InputStreamReader(InputStream in,CharsetDecoder dec) | 创建一个使用给定charset解码器的InputStreamReader。 | 
| #### 3.1.2 案例:将字节输入流转为字符输入流 | ||
| ``` | ||
| package person.xsc.practice; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| public class InputStreamReaderDemo { | 
public static void main(String[] args) {
     try {
            FileInputStream fis = new FileInputStream("C:\\Users\\你是小朱老师呀\\Desktop\\speech.txt");  
            InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); 
            int n = 0;
            char[] ch = new char[10];
            while((n=isr.read(ch))!=-1) { //第二种读法,一次读取ch.length个字节
                String s = new String(ch,0,n); //参数含义与write(b,off,n)相同
                System.out.print(s);
            }
            isr.close();
            fis.close();    
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}### 3.2 字节字符转换流(写)——OutputStreamWriter
#### 3.2.1 OutputStreamWriter类中的方法
|变量和类型|方法|说明|
|-|-|-|
|void|flush()|刷新流。|
|String|getEncoding()|返回此流使用的字符编码的名称。|
|void|write(char[] cbuf, int off, int len)|写一个字符数组的一部分。|
|void|write(int c)|写一个字符。|
|void|write(String str, int off, int len)|写一个字符串的一部分。|
|构造方法|public OutputStreamWriter(OutputStream out,String charsetName)throws UnsupportedEncodingException|创建使用指定charset的OutputStreamWriter。|
|构造方法|public OutputStreamWriter(OutputStream out)|创建使用默认字符编码的OutputStreamWriter。|
|构造方法|public OutputStreamWriter(OutputStream out,Charset cs)|创建使用给定charset的OutputStreamWriter。|
|构造方法|public OutputStreamWriter(OutputStream out,CharsetEncoder enc)|创建使用给定charset编码器的OutputStreamWriter。|
#### 3.2.2 案例:将字节输出流转为字符输出流package person.xsc.practice;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class InputStreamReaderDemo {
    public static void main(String[] args) {
         try {
                FileOutputStream fos = new FileOutputStream("C:\Users\你是小朱老师呀\Desktop\speech.txt");
                OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); 
                osw.write("你好,编程世界");
                osw.close();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
## 4. 对象序列化
对象序列化就是把一个对象变为二进制得数据流的一种方法。如果一个类的对象想要被序列化,则对象所在的类必须实现java.io.Serializable接口。
**序列化:**
把Java对象转换为字节序列的过程(写对象)。
**反序列化:**
把字节序列恢复为Java对象的过程(读对象)。package person.xsc.praticeIII; import java.io.Serializable; public class Person implements Serializable{ private String name ; // 声明name属性,但是此属性不被序列化 private int age ; // 声明age属性 public Person(String name,int age){ // 通过构造设置内容 this.name = name ; this.age = age ; } public String toString(){ // 覆写toString()方法 return "姓名:" + this.name + ";年龄:" + this.age ; } }
### 4.1 对象输出流ObjectOutputStreampackage person.xsc.praticeIII; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.OutputStream; public class ObjectOutTest { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File f = new File("C:\Users\你是小朱老师呀\Desktop\speech.txt") ; // 定义保存路径 ObjectOutputStream oos = null ; // 声明对象输出流 OutputStream out = new FileOutputStream(f) ; // 文件输出流 oos = new ObjectOutputStream(out) ; oos.writeObject(new Person("张三",30)) ; // 保存对象 oos.close() ; // 关闭 } }
### 4.2 对象输入流ObjectInputStreampackage person.xsc.praticeIII; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; public class ObjectOutTest { public static void main(String[] args) throws IOException, ClassNotFoundException { // TODO Auto-generated method stub File f = new File("C:\Users\你是小朱老师呀\Desktop\speech.txt") ; // 定义保存路径 ObjectInputStream ois = null ; // 声明对象输入流 InputStream input = new FileInputStream(f) ; // 文件输入流 ois = new ObjectInputStream(input) ; // 实例化对象输入流 Object obj = ois.readObject() ; // 读取对象 ois.close() ; // 关闭 System.out.println(obj) ; } }
```

