package m1;
import jdk.nashorn.internal.ir.IfNode;
import java.io.*;
import java.util.Arrays;
import java.util.concurrent.TransferQueue;
/**
* @author Yzt
* @version 1.0
* @date 2021/10/25 20:46
*/
public class IO1 {
public static void main(String[] args) {
// f1();//普通字节流读取
// f2();//高效字节流读取
// f3();//普通字符读取
// f4();//高效字符读取
// f5();//普通字节输出流
// f6();//高效字节输出流
// f7();//普通字符输出流
// f8();//高效字符输出流
/**
* 序列化是指把程序中给的Java对象,永久保存的磁盘当中,相当于写出的过程 对应out ObjectOutputStream
* 反序列化与之相反 对应in ObjectInputStream
* 注意:反序列化时,最好是一次序列化一次反序列化,因为若是有改动的话会导致UID不一致导致报错,或在类中指定uid,这样就不会报错了*/
f9();//测试序列化
f10();//测试反序列化
}
private static void f10() {
ObjectInputStream in=null;
try {
in=new ObjectInputStream(new FileInputStream("G:\\1.txt"));
Object o = in.readObject();
System.out.println(o);//student类需要tostring,要不然打印的是地址值
System.out.println("反序列化成功");
} catch (Exception e) {
System.out.println("反序列化失败");
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void f9() {
ObjectOutputStream out=null;
try {
out=new ObjectOutputStream(new FileOutputStream("G:\\1.txt"));
//准备对象
Student student = new Student("牛魔",13,"火焰山","男");
out.writeObject(student);
System.out.println("序列化成功");
} catch (IOException e) {
System.out.println("序列化失败");
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void f8() {
BufferedWriter out=null;
try {
out=new BufferedWriter(new FileWriter("G:\\2.txt",true));
out.write(123);
System.out.println("输出成功");
out.flush();//防止数据遗漏
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void f7() {
Writer out=null;
try {
out=new FileWriter("G:\\2.txt",true);
out.write(100);
System.out.println("输出成功");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void f6() {
BufferedOutputStream out=null;
try {
out=new BufferedOutputStream(new FileOutputStream("G:\\1.txt",true));
out.write(99);
System.out.println("输出成功");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void f5() {
OutputStream out=null;
try {
out=new FileOutputStream("G:\\1.txt",true);//默认直接覆盖原数据,要开启append
out.write(97);
System.out.println("输出成功");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void f4() {
BufferedReader in=null;
try {
in=new BufferedReader(new FileReader(new File("G:\\1.txt")));
int b;
while ((b=in.read())!=-1){
System.out.println(b);
}
}catch (Exception e){
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void f3() {
//创建一个初始化reader对象
Reader in=null;
try {
in=new FileReader(new File("G:\\1.txt"));
int b;
while ((b=in.read())!=-1){
System.out.println(b);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void f2() {
BufferedInputStream in=null;//默认容量为8192字节
try {
in=new BufferedInputStream(new FileInputStream("G:\\1.txt"));
int b;
while ((b=in.read())!=-1){
System.out.println(b);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void f1() {
//创建字节输入流读取
InputStream in=null;//若不定义此成员变量,则关流时无法引用in
try {
// InputStream in=new FileInputStream(new File("G:\\1.txt"));
in=new FileInputStream("G:\\1.txt");
// System.out.println(in.read());
// System.out.println(in.read());
// System.out.println(in.read());
// System.out.println(in.read());//-1若以达到文件末尾,则返回-1
int b;//定义变量接受数据,不定义b的话会跳着读
while ((b=in.read())!=-1){
System.out.println(b);
}//循环遍历读取信息
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//测试序列化与反序列化
class Student implements Serializable{//必须实现可序列化接口
private static final long serialVersionUID = 3096210940031231086L;
private String name;
private int age;
private String addr;
private String gender;
public Student() {
System.out.println("我是student的无参构造");
}
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 String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Student(String name, int age, String addr, String gender) {
System.out.println("我是student的全参构造");
this.name = name;
this.age = age;
this.addr = addr;
this.gender = gender;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", addr='" + addr + '\'' +
", gender='" + gender + '\'' +
'}';
}
}