字符字节流
public static void main(String[] args) {
// 程序内和程序外的数据交换操作
// java.io 包,包含了所有与 io 流相关的类
// File,只对本身做操作,不涉及内容
/*
* 字节流:一次读取一个字节
* InputStream:字节输入流
* OutputStream:字节输出流
* 这两个都是抽象类,当以了文件读写的流程,需要子类实现具体的功能
* FileInputStream:
* read(bytes):读取数据到 byte[] 数组中,返回值:
* 1. 返回读取到数据的长度
* 2. 读取不到数据就返回 -1
* FileOutputStream:
* 构造方法参数:
* true:追加,在原有数据后新增内容
* false:覆盖,覆盖原有的数据
* write(bytes, 0, length):把指定的数据写入到文件中
*
* 字符流:一次读取一个字符
* Reader:字符输入流
* Writer:字符输出流
* 这两个也是抽象类,使用子类,使用流程和字节流是一样的
* 字节流使用 byte[],字符流使用 char[]
* FileReader:
* FileWriter:
* 换行标志:\n\r
*
* 使用 try() {} catch 自动关闭流,需要实现 Closeable 接口
*
* 带缓冲的流:积攒到足够的数据后,读取到程序或者写入到磁盘
* 可以减少磁盘的读写操作,提高程序的效率
*
* BufferedInputStream:InputStream
* 为 参数 InputStream 添加一个缓冲区功能
* 就可以为任意一个字节输入流添加一个缓冲区功能,读取数据仍然使用的 参数 InputStream
*
* 属于设计模式中的:装饰模式,不修改原有类代码的前提下,为原有类添加新的功能
* 通过 BufferedInputStream 不需要修改 FileInputStream 任何代码既可为它添加缓冲区功能
* 设计模式:前人帮我们总结好的组织类之间关系的最优模板,方便我们写出易维护,可扩展的代码
* 经典的模式有 23 种
*
* BufferedOutputStream:OutputStream
*
* BufferedReader:Reader
* BufferedWriter:Writer
*/
// 流的子类非常多,每种子类都针对不同的数据进行读写操作
try (FileInputStream fileInputStream = new FileInputStream("D:/JavaTest/1.txt");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);) {
// 为 fileInputStream 增加缓冲区功能,默认缓冲区大小是 8192
// 积攒满 8192 数据后进行一次读写操作
byte[] bytes = new byte[1024]; // 设定一次读取的数据大小
int length = 0;
while ((length = bufferedInputStream.read(bytes)) != -1) {
// 使用数据
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
对象序列化
// 对象的读写:
/*
* 实现 Serializable 接口
* 这个接口中没有任何方法,它就是一个标志,表明这个类可以进行读写操作
*
* 2. 使用 ObjectInputStream 或者 ObjctOutputStream 进行对象的读写操作
* ObjectInputStream:从磁盘上读取对象
* ObjctOutputStream:把对象保存在磁盘上
*/
public class Soft implements Serializable {
private String name;
private double version;
private LocalDateTime installedTime;
public Soft() {
super();
}
public Soft(String name, double version, LocalDateTime installedTime) {
super();
this.name = name;
this.version = version;
this.installedTime = installedTime;
}
@Override
public String toString() {
return "Soft [name=" + name + ", version=" + version + ", installedTime=" + installedTime + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getVersion() {
return version;
}
public void setVersion(double version) {
this.version = version;
}
public LocalDateTime getInstalledTime() {
return installedTime;
}
public void setInstalledTime(LocalDateTime installedTime) {
this.installedTime = installedTime;
}
}
public static void main(String[] args) {
// 数据持久化:把程序中的数据,存储到硬盘上的操作。
// 把 softs 集合写入到硬盘上的 softs.data 文件中
// 需要保存的数据必须支持对象序列化
// 对象序列化:把对象转换为二进制数据
// 对象反序列化:把二进制数据转换为对象
// 实现 Serializable 接口,
// 就说明这个类支持序列化和反序列化,能够在对象和二进制之间进行相互转换
// 很重要数据无论是保存还是网络传输都要用到
// transient 修饰的变量,不会进行序列化操作,保存的时候会忽略
People people = new People("张三", 40, true);
People people2 = new People("李四", 25, true);
People people3 = new People("王五", 30, false);
// 存储多个对象,把多个对象放入集合中,把集合保存到磁盘
// 只要集合中的对象,实现了 Serializable 接口,就可以把集合写入到磁盘
ArrayList<People> arrayList = new ArrayList<People>();
arrayList.add(people);
arrayList.add(people2);
arrayList.add(people3);
try (FileOutputStream fileOutputStream = new FileOutputStream("D:/JavaTest/9.text");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);){
objectOutputStream.writeObject(arrayList);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try (FileInputStream fileInputStream = new FileInputStream("D:/JavaTest/9.text");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);){
// 存取的内容类型要保持一致,存什么,取什么,内容不会产生改变
ArrayList<People> arrayList2 = (ArrayList<People>) objectInputStream.readObject();
for (People people4 : arrayList2) {
System.out.println(people4);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
properties
/*
//test.properties文件中
age=888888888888888
name=ZhangSan
School=Zhiyou
*/
public class test {
public static void main(String[] args) {
method();
}
public static void method() {
Properties properties = new Properties();
try (FileInputStream fileInputStream = new FileInputStream("test.properties");) {
properties.load(fileInputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
String age = properties.getProperty("age");
System.out.println(age);
}
}