7.5 KiB
7.5 KiB
IO流体系结构
字节流:
FileOutPutStream 字节输出流
构造方法和成员方法和注意事项:
成员方法
IO流的异常处理方式
示例:
public static void main(String[] args) throws IOException {
//记住输出流一定要关闭,使用try catch保证不管如何都会执行关闭
//创建字节输出流对象是,可以传两个参数第一个是字符串或者是file对象,第二个是打开或关闭复写的开关
//try小括号里的对象必须实现了AutoCloseAble接口
try (FileOutputStream fos = new FileOutputStream("D:\\A.txt",true);){
fos.write(97);
fos.write(97);
fos.write("八奈见杏菜".getBytes());
}catch (IOException e){
e.printStackTrace();
}
}
FileInPutStream字节输入流:
构造和成员方法:
案例:copy数据:
/**
拷贝文件
*/
public static void main(String[] args) throws IOException {
//创建输入流对象读取要复制的数据
FileInputStream fis = new FileInputStream("C:\\Users\\asus\\Pictures\\老八.jpg");
byte[] bys= new byte[1024];//定义一个空字节数组用来装数据
//创建输出流对象粘贴读取的数据
FileOutputStream fos= new FileOutputStream("D:\\老八.jpg");
int len;
while ((len = fis.read(bys)) != -1){
fos.write(bys,0, len);
}
//记得关闭流
fis.close();
fos.close();
}
字节缓冲流:(未听)
小知识点:字符编码:
编码和解码:
总结:(可记)
FileWriter字符输出流
成员方法:
字符缓冲流:
构造方法:
特有成员方法:
重点:
案例:
三组对象写入文件后读取文件并封装成对象打印
public static void main(String[] args) throws IOException {
Student stu1 = new Student("杏菜",15);
Student stu2 = new Student("梦子",16);
Student stu3 = new Student("佳树",17);
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\测试.txt"));
bw.write(stu1.getName()+"-"+stu1.getAge());
bw.newLine();
bw.write(stu2.getName()+"-"+stu2.getAge());
bw.newLine();
bw.write(stu3.getName()+"-"+stu3.getAge());
bw.newLine();
//写入程序三组数据
bw.close();
//读取三组数据
BufferedReader br = new BufferedReader(new FileReader("D:\\测试.txt"));
String line;
while ((line = br.readLine()) != null){
String[] split = line.split("-");
Student stu = new Student(split[0],Integer.parseInt(split[1]));
System.out.println(stu);
}
}
转换流:
作用:
总结:
序列化流:
构造方法:
序列化流的操作:
注意事项:
transient 瞬态关键字类中某些成员变量不想被序列化操作即可加上此关键字
注意:要是序列化多次对象,有两种方式
try catch捕获异常,来结束循环
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\a.txt"));
while (true){
try {
Object o = ois.readObject();
System.out.println(o);
} catch (EOFException O) {
break;
}
}
ois.close();
}
private static void write() throws IOException {
Student stu1 = new Student("杏菜",15);
Student stu2 = new Student("佳树",16);
Student stu3 = new Student("梦子",17);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\a.txt"));
oos.writeObject(stu1);
oos.writeObject(stu2);
oos.writeObject(stu3);
oos.close();
}
只序列化一次,将对象装入一个集合中,读取和写入只进行一次
public static void main(String[] args) throws IOException, ClassNotFoundException {
//运用集合,使其只读取(序列化和反序列化一次)
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\a.txt"));
ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
for (Student student : list) {
System.out.println(student);
}
ois.close();
}
private static void write() throws IOException {
Student stu1 = new Student("杏菜", 15);
Student stu2 = new Student("佳树", 16);
Student stu3 = new Student("梦子", 17);
//创建一个集合将对象装入集合并序列化操作一次写入文件
ArrayList<Student> list = new ArrayList<>();
Collections.addAll(list, stu1, stu2, stu3);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\a.txt", true));
oos.writeObject(list);
oos.close();
}
打印流:
基本使用:
字符打印流:
properties
和io有关的方法:
本质是一个map集合
总结:





