转换流
字符流中和编码解码问题相关的两个类
- InputStreamReader:是从字节流到字符流的桥梁,父类是Reader 它读取字节,并使用指定的编码将其解码为字符 它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集
- OutputStreamWriter:是从字符流到字节流的桥梁,父类是Writer 是从字符流到字节流的桥梁,使用指定的编码将写入的字符编码为字节 它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集
转换流读写数据
构造方法
| 方法名 | 说明 |
|---|---|
| InputStreamReader(InputStream in) | 使用默认字符编码创建InputStreamReader对象 |
| InputStreamReader(InputStream in,String chatset) | 使用指定的字符编码创建InputStreamReader对象 |
| OutputStreamWriter(OutputStream out) | 使用默认字符编码创建OutputStreamWriter对象 |
| OutputStreamWriter(OutputStream out,String charset) | 使用指定的字符编码创建OutputStreamWriter对象 |
package com.whs.test01;import java.io.*;public class Test12 {public static void main(String[] args) throws IOException {//OutputStreamWriterOutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("E:\\data.txt"));OutputStreamWriter outputStreamWriter1 = new OutputStreamWriter(new FileOutputStream("E:\\data.txt",true));//写入outputStreamWriter.write("一叶知秋");outputStreamWriter1.write("我的地盘我做主");//关闭连接outputStreamWriter.close();outputStreamWriter1.close();//InputStreamReaderInputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("E:\\data.txt"));char[] chars = new char[1024];int len;while ((len=inputStreamReader.read(chars))!=-1){System.out.println(new String(chars,0,len));System.out.println(chars);}//释放inputStreamReader.close();}}
对象操作流
对象序列化流
对象序列化介绍
- 对象序列化:就是将对象保存到磁盘中,或者在网络中传输对象
- 这种机制就是使用一个字节序列表示一个对象,该字节序列包含:对象的类型、对象的数据和对象中存储的属性等信息
- 字节序列写到文件之后,相当于文件中持久保存了一个对象的信息
- 反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化
对象序列化流: ObjectOutputStream
- 将Java对象的原始数据类型和图形写入OutputStream。 可以使用ObjectInputStream读取(重构)对象。 可以通过使用流的文件来实现对象的持久存储。 如果流是网络套接字流,则可以在另一个主机上或另一个进程中重构对象
构造方法
| 方法名 | 说明 |
|---|---|
| ObjectOutputStream(OutputStream out) | 创建一个写入指定的OutputStream的ObjectOutputStream |
序列化对象的方法
| 方法名 | 说明 |
|---|---|
| void writeObject(Object obj) | 将指定的对象写入ObjectOutputStream |
学生类
public class Student implements Serializable {
private String name;
private int age;
}
测试类
package com.whs.test01;
import com.whs.entity.Student;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Test13 {
public static void main(String[] args) throws IOException {
//1. ObjectOutputStream(OutputStream out):创建一个写入指定的OutputStream的ObjectOutputStream
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("E:\\student.txt"));
//2. 创建对象
Student student = new Student("王汉生",18);
//3. void writeObject(Object obj):将指定的对象写入ObjectOutputStream
objectOutputStream.writeObject(student);
//4.释放资源
objectOutputStream.close();
}
}
注意事项
- 一个对象要想被序列化,该对象所属的类必须必须实现Serializable 接口
- Serializable是一个标记接口,实现该接口,不需要重写任何方法
对象反序列化流
对象反序列化流: ObjectInputStream
- ObjectInputStream反序列化先前使用ObjectOutputStream编写的原始数据和对象
构造方法
| 方法名 | 说明 |
|---|---|
| ObjectInputStream(InputStream in) | 创建从指定的InputStream读取的ObjectInputStream |
反序列化对象的方法
| 方法名 | 说明 |
|---|---|
| Object readObject() | 从ObjectInputStream读取一个对象 |
package com.whs.test01;
import com.whs.entity.Student;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class Test14 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//1. ObjectInputStream(InputStream in):创建从指定的InputStream读取的ObjectInputStream
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("E:\\student.txt"));
//2. Object readObject():从ObjectInputStream读取一个对
Object object = objectInputStream.readObject();
//3. 接受对象
Student student = (Student)object;
//4. 输出对象信息
System.out.println(student.toString());
//5. 关闭连接
objectInputStream.close();
}
}
serialVersionUID和transient
serialVersionUID—————用对象序列化流序列化了一个对象后
假如我们修改了对象所属的类文件,读取数据会不会出问题呢?
会出问题,会抛出InvalidClassException异常
如果出问题了,如何解决呢?
重新序列化,给对象所属的类加一个serialVersionUID
private static final long serialVersionUID = 42L;
transient
如果一个对象中的某个成员变量的值不想被序列化,又该如何实现呢?
给该成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程
package com.whs.entity;
import java.io.Serializable;
public class Student implements Serializable { //实现序列化
private static final long serialVersionUID = 42L;
private String name;
//private int age;
private transient int age; //age不想被序列化
}
package com.whs.test01;
import com.whs.entity.Student;
import java.io.*;
public class Test15 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
wirte();
read();
}
//反序列化
public static void read() throws IOException, ClassNotFoundException {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("E:\\student.txt"));
Student object = (Student)objectInputStream.readObject();
System.out.println(object.toString());
objectInputStream.close();
}
//序列化
public static void wirte() throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("E:\\student.txt"));
Student student = new Student("cc",20);
objectOutputStream.writeObject(student);
objectOutputStream.close();
}
}
对象操作流练习
案例需求创建多个学生类对象写到文件中,再次读取到内存中
实现步骤
- 创建序列化流对象
- 创建多个学生对象
- 将学生对象添加到集合中
- 将集合对象序列化到文件中
- 创建反序列化流对象
- 将文件中的对象数据,读取到内存中 ```java package com.whs.test01;
import com.whs.entity.Student;
import java.io.*; import java.util.ArrayList;
public class Test16 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//序列化
//1.创建序列化流对象
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(“E:\student.txt”));
ArrayList
//反序列化
//5.创建反序列化流对象
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("E:\\student.txt"));
//6.将文件中的对象数据,读取到内存中
ArrayList<Student> object = (ArrayList<Student>)objectInputStream.readObject();
for (Student student:object) {
System.out.println(student.toString());
}
//7.关闭连接
objectInputStream.close();
objectOutputStream.close();
}
}
<a name="lCzHz"></a>
# Properties集合
<a name="Y7F9u"></a>
## Properties作为Map集合的使用
Properties介绍
- 是一个Map体系的集合类
- Properties可以保存到流中或从流中加载
- 属性列表中的每个键及其对应的值都是一个字符串
**Properties基本使用**
```java
package com.whs.test01;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Test17 {
public static void main(String[] args) {
//创建集合对象
//Properties<String,String> properties = new Properties<String,String>();//错误
Properties properties = new Properties();
//存储元素
properties.put("itheima001", "佟丽娅");
properties.put("itheima002", "赵丽颖");
properties.put("itheima003", "刘诗诗");
//遍历集合
Set<Map.Entry<Object, Object>> entries = properties.entrySet();
for (Map.Entry<Object, Object> entry: entries) {
Object value = entry.getValue();
Object key = entry.getKey();
System.out.println(key+"-------"+value);
}
}
}
Properties作为Map集合的特有方法
特有方法
| 方法名 | 说明 |
|---|---|
| Object setProperty(String key, String value) | 设置集合的键和值,都是String类型,底层调用 Hashtable方法 put |
| String getProperty(String key) | 使用此属性列表中指定的键搜索属性 |
| Set |
从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串 |
package com.whs.test01;
import java.util.Properties;
import java.util.Set;
public class Test18 {
public static void main(String[] args) {
//创建集合对象
Properties properties = new Properties();
//Object setProperty(String key, String value):设置集合的键和值,可更新
properties.setProperty("whs1","cc");
properties.setProperty("whs2","whoose");
properties.setProperty("whs3","猪肉的魅力");
//String getProperty(String key):使用此属性列表中指定的键搜索属性
System.out.println(properties.getProperty("whs1"));
System.out.println(properties.getProperty("whs2"));
System.out.println(properties.getProperty("whs3"));
//Set<String> stringPropertyNames():从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
Set<String> names = properties.stringPropertyNames();
for (String key:names) {
String value = properties.getProperty(key);
System.out.println(key+"--------"+value);
}
}
}
Properties和IO流相结合的方法
和IO流结合的方法
| 方法名 | 说明 |
|---|---|
| void load(Reader reader) | 从输入字符流读取属性列表(键和元素对) |
| void store(Writer writer, String comments) | 将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流 |
package com.whs.test01;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Test19 {
public static void main(String[] args) throws IOException {
myStore();
myLoad();
}
private static void myLoad() throws IOException {
Properties properties = new Properties();
//void load(Reader reader):
FileReader fileReader = new FileReader("E:\\student。txt");
properties.load(fileReader);
fileReader.close();
System.out.println(properties.getProperty("itheima001"));
System.out.println(properties.getProperty("itheima002"));
System.out.println(properties.getProperty("itheima003"));
}
private static void myStore() throws IOException {
Properties properties = new Properties();
properties.put("itheima001", "佟丽娅");
properties.put("itheima002", "赵丽颖");
properties.put("itheima003", "刘诗诗");
//void store(Writer writer, String comments)
FileWriter fileWriter = new FileWriter("E:\\student。txt");
properties.store(fileWriter,null);
fileWriter.close();
}
}
Properties集合练习
案例需求在Properties文件中手动写上姓名和年龄,读取到集合中,将该数据封装成学生对象,写到本地文件
实现步骤
- 创建Properties集合,将本地文件中的数据加载到集合中
- 获取集合中的键值对数据,封装到学生对象中
- 创建序列化流对象,将学生对象序列化到本地文件中 ```java package com.whs.entity;
import java.io.Serializable;
public class Student implements Serializable { //实现序列化 private static final long serialVersionUID = 42L; private String name; private int age; //private transient int age; //age不想被序列化 }
读取文件的内容
```java
package com.whs.test01;
import com.whs.entity.Student;
import java.io.*;
import java.util.Properties;
public class Test20 {
public static void main(String[] args) throws IOException {
//1.创建Properties集合,将本地文件中的数据加载到集合中
Properties properties = new Properties();
FileReader fileReader = new FileReader("E:\\student.properties");
properties.load(fileReader);
fileReader.close();
//2.获取集合中的键值对数据,封装到学生对象中
String name = properties.getProperty("name");
int age = Integer.parseInt(properties.getProperty("age"));
Student student = new Student(name,age);
System.out.println(student.toString());
//3.创建序列化流对象,将学生对象序列化到本地文件中
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("E:\\student.txt"));
objectOutputStream.writeObject(student);
objectOutputStream.close();
}
}
