对象流
对象流:Object0utputStream/0bjectInputStream
增强了缓冲区功能
增强了读写8种基本数据类型和字符串功能
增强了读写对象的功能
1.read0bject() 从流中读取一个对象
2.write0bject(0bject obj) 向流中写入一个对象
使用流传输对象的过程称为序列化、反序列化
eg :
public class Student implements Serializable{
/**
* serialVersionUID:序列化版本号ID,作用
*/
private static final long serialVersionUID = 100L;
private String name;
private transient int age;
public static String country = "中国";
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name=‘" + name + ‘\‘‘ +
", age=" + age +
‘}‘;
}
}
/**
* 使用objectOutputStream实现对象的序列化
* 要求:
* 1.序列化类必须要实现Serializable接口
* 2.序列化类中对象属性要求实现Serializable接口
* 3.序列化版本号ID,保证序列化的类和反序列化的类是同一个类
* 4.使用transient修饰属性(瞬间的),这个属性不能序列化
* 5.静态属性不能序列化
* 6.序列化多个对象,可以借助集合实现
* @author
*/
public class Demo06 {
public static void main(String[] args) throws IOException {
//1.创建对象流
FileOutputStream fos = new FileOutputStream("g:\\stu.bin");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//2.序列化(写入操作)
Student zhangsan = new Student("张三",20);
Student lisi = new Student("李四",22);
ArrayList<Student> list = new ArrayList<>();
list.add(zhangsan);
list.add(lisi);
oos.writeObject(list);
//oos.writeObject(zhangsan);
//oos.writeObject(lisi);
//3.关闭
oos.close();
System.out.println("序列化完毕");
}
}
public class Demo07 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//1.创建对象流
FileInputStream fis = new FileInputStream("g:\\stu.bin");
ObjectInputStream ois = new ObjectInputStream(fis);
//2.读取文件(反序列化) (读取重构成对象)
//Student s = (Student)ois.readObject();//不能多个
//Student s1 = (Student)ois.readObject();//不能多个
ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
//3.关闭
ois.close();
System.out.println("执行完毕");
//System.out.println(s.toString());
//System.out.println(s1.toString());
System.out.println(list.toString());
}
}
打印流
PrintWriter:
封装了print() / println()方法,支持写入后换行
支持数据原样打印
eg :
public class Demo07 {
public static void main(String[] args) throws Exception {
//1.创建打印流
PrintWriter pw = new PrintWriter("g:\\print.txt");
//2.打印
pw.println(97);
pw.println(true);
pw.println(3.14);
pw.println(‘a‘);
//3.关闭
pw.close();
System.out.println("执行完毕");
}
}
转换流
桥转换流:InputStreamReader/OutputStreamWriter
可将字节流转换为字符流
可设置字符的编码方式
eg :
public class Demo01 {
public static void main(String[] args) throws Exception {
//1.创建InputStreamReader对象
FileInputStream fis = new FileInputStream("g:\\write.txt");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
//2.读取文件
int data = 0;
while((data = isr.read())!=-1){
System.out.print((char)data);
}
//3.关闭
isr.close();
}
}
public class Demo02 {
public static void main(String[] args) throws Exception {
//1.创建OutputStreamWriter
FileOutputStream fos = new FileOutputStream("g:\\info.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
//2.写入
for (int i = 0; i < 10; i++) {
osw.write("我爱故乡\r\n");
osw.flush();
}
//3.关闭
osw.close();
System.out.println("执行换行");
}
}
字符编码
ISO-8859-1收录除ASCII外,还包括西欧、希腊语、泰语、阿拉伯语、希伯来语对应的文字符号
UTF-8 针对Unicode码表的可变长度字符编码
GB2312 简体中文
GBK 简体中文、扩充
BIG5台湾,繁体中文
当编码方式和解码方式不一致时,会出现乱码
原文:https://www.cnblogs.com/linhtx212318293/p/14539042.html