对象处理流
对象处理流
1.序列化:在保持数据时保持数据的值和数据类型。
2.反序列化:在恢复数据的时候恢复数据的值和类型。
序列化读写顺序要一致。
要求实现序列化和反序列化的类实现Serializable接口,serialVersionUID 序列化版本号,可以提高兼容性。
序列化对象时默认将里面所有的方法和属性都进行序列化,static和transient修饰除外。
序列化对象时要求里面属性类型也实现序列化接口。
序列化具有可继承性,某类实现了序列化,其子类也默认实现序列化。
ObjectOutputStream
ObjectOutputStream将Java对象的基本数据类型和图形写入OutputStream。
可以使用ObjectInputStream读取(重构)对象。通过在流中使用文件可以实现对象的持久存储。
如果流是网络套接字流,则可以在另一台主机上或另一个进程中重构对象。
构造方法:
1.ObjectOutputStream(OutputStream out) 创建写入指定OutputStream的ObjectOutputStream。
成员方法:
1.write(byte[] buf) 写入一个byte数组。
2.write(byte[] buf, int off, int len) 写入字节的子数组。
3.writeObject(Object obj) 将指定的对象写入 ObjectOutputStream。
4.flush() 刷新该流的缓冲。
5.close() 关闭流。
ObjectInputStream
ObjectInputStream对以前使用ObjectOutputStream写入的基本数据和对象进行反序列化(重构)。
构造方法:
1.ObjectInputStream(InputStream in) 创建从指定 InputStream 读取的 ObjectInputStream。
成员方法:
1.read() 读取数据字节。
2.read(byte[] buf, int off, int len) 读入byte数组。
3.readObject() 从ObjectInputStream 读取对象。
4.close() 关闭输入流。
对象序列化
public static void main(String[] args) {
Person person1 = new Person("唐纳德 特朗普",'男',74);
Person person2 = new Person("乔 拜登",'男',78);
List<Person> persons = new ArrayList<>();
persons.add(person1);
persons.add(person2);
ObjectOutputStream oos = null;
try {
String filePath = "C:\\Users\\ms674\\Desktop\\File\\DDD\\person.bean";
//创建序列化流对象,指定写入序列化流(本地序列化源文件)
oos = new ObjectOutputStream(new FileOutputStream(new File(filePath),true));
oos.writeObject(persons);//序列化对象
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
反序列化
public static void main(String[] args) {
ObjectInputStream ois = null;
try {
String filePath = "C:\\Users\\ms674\\Desktop\\File\\DDD\\person.bean";
//创建反序列化流对象,读取本地序列化文件
ois = new ObjectInputStream(new FileInputStream(new File(filePath)));
List<Person> persons = (List<Person>)ois.readObject();
for (Person person : persons) {
System.out.println(person.getUsername());
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
标准输入输出流
//System.in 类的 public final static InputStream in = null
//System.in 编译类型 InputStream
//System.in 运行类型 BufferedInputStream
//标准输入 - 键盘
Scanner scanner = new Scanner(System.in);
//System.out 类的 public final static PrintStream in = null
//System.in 编译类型 PrintStream
//System.in 运行类型 PrintStream
//标准输入 - 显示器
System.out.println(scanner.nextLine());
转换流
InputStreamReader
InputStreamReader是字节流通向字符流的桥梁:它使用指定的charset读取字节并将其解码为字符。
它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。
为了达到最高效率,通常在BufferedReader内包装 InputStreamReader。
构造方法:
1.InputStreamReader(InputStream in, Charset cs) 创建使用给定字符集的InputStreamReader。
2.InputStreamReader(InputStream in, String charsetName) 创建使用指定字符集的InputStreamReader。
成员方法:
1.read() 读取单个字符。
2.read(char[] cbuf, int offset, int length) 将字符读入数组中的某一部分。
3.getEncoding() 返回此流使用的字符编码的名称。
4.close() 关闭该流并释放与之关联的所有资源。
public static void main(String[] args) {
BufferedReader br = null;
try {
String filePath = "C:\\Users\\ms674\\Desktop\\File\\DDD\\file1-iso-8859-1.txt";
///InputStreamReader(new FileInputStream(new File(filePath)), "GBK");
InputStreamReader isr
= new InputStreamReader(new FileInputStream(new File(filePath)), StandardCharsets.UTF_8);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
OutputStreamWriter
OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的charset将要写入流中的字符编码成字节。
它使用的字符集可以由名称指定或显式(StandardCharsets枚举)给定,否则将接受平台默认的字符集。
每次调用 write() 方法都会导致在给定字符(或字符集)上调用编码转换器。在写入底层输出流之前,
得到的这些字节将在缓冲区中累积。可以指定此缓冲区的大小,不过,默认的缓冲区对多数用途来说已足够大。
注意,传递给write()方法的字符没有缓冲。为了获得最高效率,可考虑将OutputStreamWriter包装到BufferedWriter中。
构造方法:
1.OutputStreamWriter(OutputStream out, Charset cs) 创建使用给定字符集的 OutputStreamWriter。
2.OutputStreamWriter(OutputStream out, String charsetName) 创建使用指定字符集的 OutputStreamWriter。
成员方法:
1.write(int c) 写入单个字符
2.write(char[] cbuf, int off, int len) 写入字符数组的某一部分。
3.write(String str, int off, int len) 写入字符串的某一部分。
4.getEncoding() 返回此流使用的字符编码的名称。
5.flush() 刷新该流的缓冲。
6.close() 关闭此流,但要先刷新它。
public static void main(String[] args) {
BufferedWriter bw = null;
try {
String filePath = "C:\\Users\\ms674\\Desktop\\File\\DDD\\file1-iso-8859-1.txt";
OutputStreamWriter osw =
new OutputStreamWriter(new FileOutputStream(new File(filePath),true), StandardCharsets.UTF_8);
bw = new BufferedWriter(osw);
bw.write("今天天气不太好啊,又不能出门了。");
bw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
打印流
PrintStream
PrintStream为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。
构造方法
1.PrintStream(File file) 创建具有指定文件且不带自动行刷新的新打印流
2.PrintStream(OutputStream out) 创建新的打印流。
成员方法:
1.println(String x) 打印String,然后终止该行,底层调用write方法。
2.write(byte[] buf, int off, int len) 将len字节从指定的初始偏移量为off的 byte 数组写入此流。
3.flush() 刷新该流的缓冲。
4.close() 关闭流。
PrintWriter
向文本输出流打印对象的格式化表示形式。此类实现在PrintStream中的所有print 方法。
它不包含用于写入原始字节的方法,对于这些字节,程序应该使用未编码的字节流进行写入。
构造方法:
1.PrintWriter() 本地显示器标准输出打印System.out
2.PrintWriter(OutputStream out) 根据现有的OutputStream创建不带自动行刷新的新 PrintWriter。
成员方法:
1.print(String s) 打印字符串。
2.flush() 刷新该流的缓冲。
3.close() 关闭该流并释放与之关联的所有系统资源。
public static void main(String[] args) throws IOException {
PrintStream out = System.out;
//默认输出位置为标准输出 - 显示器
out.println("hello");//底层调用writer
out.write("java".getBytes());
//修改输出打印到文件
String filePath = "C:\\Users\\ms674\\Desktop\\File\\DDD\\print.txt";
System.setOut(new PrintStream(filePath));
System.out.println("Hello C++");//输出到print.txt文件
out.close();
//标准输出
PrintWriter printWriter = new PrintWriter(System.out);
printWriter.write("hello printWriter");
String filePath2 = "C:\\Users\\ms674\\Desktop\\File\\DDD\\print.txt";
PrintWriter file = new PrintWriter(new FileWriter(filePath2, true));
file.write("你好");
file.flush();//刷新缓冲区
file.close();//关闭输出流,默认执行一次flush
}
数据流
DataInputStream
数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本Java数据类型。
应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。
构造方法:
1.DataInputStream(InputStream in) 使用指定的底层InputStream创建一个DataInputStream。
成员方法:
1.read(byte[] b, int off, int len) 从包含的输入流中将最多len个字节读入一个byte 数组中。
2.readUTF() 读入一个已使用 UTF-8 修改版格式编码的字符串。
DataOutputStream
数据输出流允许应用程序以适当方式将基本Java数据类型写入输出流中。
然后,应用程序可以使用数据输入流将数据读入。
构造方法:
1.DataOutputStream(OutputStream out) 创建一个新的数据输出流,将数据写入指定基础输出流。
成员方法:
1.write(byte[] b, int off, int len) 将指定byte数组中从偏移量off开始的len个字节写入基础输出流。
2.writeUTF(String str) 以与机器无关方式使用UTF-8修改版编码将一个字符串写入基础输出流。
3.flush() 清空此数据输出流。
4.close() 关闭流,关闭之前刷新缓冲区。
二进制文件拷贝案例
public static void main(String[] args) {
DataInputStream dis = null;
DataOutputStream dos = null;
try {
String sourceFile = "D:\\Browser\\jdk-8u291-linux-x64.tar.gz";
String copyFile = "C:\\Users\\ms674\\Desktop\\File\\java\\jdk-8u291-linux-x64.tar.gz";
dis = new DataInputStream(new FileInputStream(sourceFile));
dos = new DataOutputStream(new FileOutputStream(copyFile));
int len;
byte[] buffer = new byte[1024];
while ((len = dis.read(buffer)) != -1) {
dos.write(buffer,0,len);
dos.flush();//刷新缓冲区
}
System.out.println("文件拷贝成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Properties工具类(彩蛋)
Properties
Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载。
属性列表中每个键及其对应值都是一个字符串。
构造方法:
1.Properties() 创建一个无默认值的空属性列表。
成员方法:
1.getProperty(String key) 用指定的键在此属性列表中搜索属性。
2.setProperty(String key, String value) 调用Hashtable的方法put。
3.store(OutputStream out, String comments) 以适合使用load(InputStream)方法加载到Properties表中的格式,将此Properties表中的属性列表(键和元素对)写入输出流。
4.load(InputStream inStream) 从输入流中读取属性列表(键和元素对)。
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
String filePath = "C:\\Users\\ms674\\Desktop\\File\\DDD\\mysql.properties";
properties.load(new FileReader(filePath));
//读取properties文件
properties.list(System.out);//标准输出
System.out.println("主机IP地址:"+properties.getProperty("ip"));
//setProperty添加properties文件属性,如果key存在修改其value
properties.setProperty("ip","47.173.56.192");
properties.setProperty("msg","汤姆日记");
properties.store(new FileWriter(new File(filePath)),null);
}