InputStreamReader是从字节流到字符流的桥梁
OutputStreamWriter是从字符流到字节流的桥梁
public class ConversionDemo1 {
public static void main(String[] args) throws IOException {
//创建转换输出流
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("convert.txt"));
osw.write("远桥之下泛莲舟");
osw.write("\r\n");
osw.write("岱岩石上松溪流");
osw.write("\r\n");
osw.write("万仞翠山梨亭在");
osw.write("\r\n");
osw.write("莫闻空谷声悠悠");
osw.write("\r\n");
osw.close();
// 创建转换输入流
InputStreamReader isr = new InputStreamReader(new FileInputStream("convert.txt"), "GBK");
int ch;
while ((ch=isr.read())!=-1){
System.out.println((char)ch);
}
isr.close();
}
}
public class ConversionDemo2 {
public static void main(String[] args) throws IOException {
// 创建转换输入流
InputStreamReader isr = new InputStreamReader(new FileInputStream("F:\\GBK编码的文件.txt"), "GBK");
//创建转换输出流
OutputStreamWriter osw =new OutputStreamWriter(new FileOutputStream("22.txt"),"UTF-8");
int ch;
while ((ch=isr.read())!=-1){
osw.write(ch);
}
//释义资源
isr.close();
osw.close();
}
}
注意 : 如果一个类对象想要被序列化 , 那么此类需要实现Serializable接口
用对象序列化流序列化了一个对象后,假如我们修改了对象所属的Javabean类,读取数据会不会出问题呢?
如果出问题了,如何解决呢?
如果一个对象中的某个成员变量的值不想被序列化,又该如何实现呢?
给该成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程
//明确:当前类对象要进行序列化或反序列化操作时,必须保证当前类有实现Serializable接口
public class User implements Serializable {
//默认:添加一个serialVersionUID(会随着序列化操作,把serialVersionUID写入到文件中)
//在从文件中读取对象时,会比较流中的serialVersionUID 和当前类中的serialVersionUID是否相同
//只要修改User类中的代码,serialVersionUID值会改变
//解决方案:手动添加serialVersionUID的值
private static final long serialVersionUID = 1L;
private String name;
private Integer age;
private transient String address;//当前成员变量不参与序列化操作
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public User(String name, Integer age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class ObjectInputStreamDemo1 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建反序列化流对象
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("user.txt"));
//读取文件中的对象
User user = (User) ois.readObject();
//释放资源
ois.close();
//测试
System.out.println(user);
}
}
public class ObjectInputStreamDemo2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建返序列化流对象
ObjectInputStream ois =new ObjectInputStream(new FileInputStream("files/userList.txt"));
//使用反序列化读取文件中的对象
List<User> userList= (List<User>) ois.readObject();
//释放资源
ois.close();
//测试
for (User user : userList) {
System.out.println(user.getName() +" "+user.getAge());
}
}
}
public class ObjectOutputStreamDemo1 {
public static void main(String[] args) throws IOException {
//创建序列化流对象
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("user.txt"));
//创建用户对象
User user =new User();
user.setName("itheima");
user.setAge(23);
user.setAddress("上海");
oos.writeObject(user);
oos.close();
}
}