1、缓冲流
前期我们学习了基本的一些流,作为IO流的入门,今天我们要见识一些更强大的流。比如能够高效读写的缓冲流,能够转换编码的转换流,能够持久化存储对象的序列化流等等。这些功能更为强大的流,都是在基本的流对象基础之上创建而来的,就像穿上铠甲的武士一样,相当于是对基本流对象的一种增强。
1.1 缓冲流概述
缓冲流,也叫高效流,是对4个基本的
FileXxx
流的增强,所以也是4个流,按照数据类型分类
名称 | 类 |
---|---|
字节缓冲流 | BufferedInputStream BufferedOutputStream |
字符缓冲流 | BufferedReader BufferedWriter |
缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率。
1.2 字节缓冲流
1.2.1 构造方法
方法名 | 说明 |
---|---|
public BufferedInputStream(InputStream in) | 创建一个 新的缓冲输入流。 |
public BufferedOutputStream(OutputStream out) | 创建一个新的缓冲输出流。 |
1.2.1 读取数据
一次读取一个字节
public class BufferedDemo { public static void main(String[] args) throws FileNotFoundException { BufferedInputStream bis = null; BufferedOutputStream bos = null; // 创建流对象 try { bis = new BufferedInputStream(new FileInputStream("jdk9.exe")); bos = new BufferedOutputStream(new FileOutputStream("copy.exe")); // 读写数据 int b; while ((b = bis.read()) != -1) { bos.write(b); } } catch (IOException e) { e.printStackTrace(); } finally{ if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
一次读取一个字节数组
public class BufferedDemo { public static void main(String[] args) throws FileNotFoundException { BufferedInputStream bis = null; BufferedOutputStream bos = null; // 创建流对象 try { bis = new BufferedInputStream(new FileInputStream("jdk9.exe")); bos = new BufferedOutputStream(new FileOutputStream("copy.exe")); // 读写数据 int len; byte[] bytes = new byte[8*1024]; while ((len = bis.read(bytes)) != -1) { bos.write(bytes, 0 , len); } } catch (IOException e) { e.printStackTrace(); } finally{ if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
1.3 字符缓冲流
1.3.1 构造方法
方法名 | 说明 |
---|---|
public BufferedReader(Reader in) | 创建一个 新的缓冲输入流。 |
public BufferedWriter(Writer out) | 创建一个新的缓冲输出流。 |
1.3.2 字符缓冲流特有方法
类名 | 方法名 | 说明 |
---|---|---|
BufferedReader | public String readLine() | 一次读取一行文字 |
BufferedWriter | public void newLine() | 写一行行分隔符,由系统属性定义符号 |
1.3.3 特有方法代码演示
public class BufferedReaderDemo { public static void main(String[] args) throws IOException { // 创建流对象 BufferedReader br = new BufferedReader(new FileReader("in.txt")); // 定义字符串,保存读取的一行文字 String line = null; // 循环读取,读取到最后返回null while ((line = br.readLine())!=null) { System.out.print(line); System.out.println("------"); } // 释放资源 br.close(); } }
public class BufferedWriterDemo throws IOException { public static void main(String[] args) throws IOException { // 创建流对象 BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt")); for(int i = 0 ; i < 5 ; i++){ // 写出数据 bw.write("HelloWorld!"); bw.newLine(); } // 释放资源 bw.close(); } }
2、转换流
2.1 字符编码和字符集
字符编码
计算机中储存的信息都是用二进制数表示的,而我们在屏幕上看到的数字、英文、标点符号、汉字等字符是二进制数转换之后的结果。按照某种规则,将字符存储到计算机中,称为编码 。反之,将存储在计算机中的二进制数按照某种规则解析显示出来,称为解码 。比如说,按照A规则存储,同样按照A规则解析,那么就能显示正确的文本符号。反之,按照A规则存储,再按照B规则解析,就会导致乱码现象。
编码:字符(能看懂的)--字节(看不懂的)
解码:字节(看不懂的)-->字符(能看懂的)
-
字符编码
Character Encoding
: 就是一套自然语言的字符与二进制数之间的对应规则。编码表:生活中文字和计算机中二进制的对应规则
xxxxx ---> 转换成看不懂的 编码
转换成看不懂的 ---> xxxx 解码
字符集
-
字符集
Charset
:也叫编码表。是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等。
计算机要准确的存储和识别各种字符集符号,需要进行字符编码,一套字符集必然至少有一套字符编码。常见字符集有ASCII字符集、GBK字符集、Unicode字符集等。
可见,当指定了编码,它所对应的字符集自然就指定了,所以编码才是我们最终要关心的。
2.2 常见的字符集
-
ASCII字符集 :
-
ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)是基于拉丁字母的一套电脑编码系统,用于显示现代英语,主要包括控制字符(回车键、退格、换行键等)和可显示字符(英文大小写字符、阿拉伯数字和西文符号)。
-
基本的ASCII字符集,使用7位(bits)表示一个字符,共128字符。ASCII的扩展字符集使用8位(bits)表示一个字符,共256字符,方便支持欧洲常用字符。
-
-
ISO-8859-1字符集:
-
拉丁码表,别名Latin-1,用于显示欧洲使用的语言,包括荷兰、丹麦、德语、意大利语、西班牙语等。
-
ISO-8859-1使用单字节编码,兼容ASCII编码
-
-
GBxxx字符集:
-
GB就是国标的意思,是为了显示中文而设计的一套字符集。
-
GB2312:简体中文码表。一个小于127的字符的意义与原来相同。但两个大于127的字符连在一起时,就表示一个汉字,这样大约可以组合了包含7000多个简体汉字,此外数学符号、罗马希腊的字母、日文的假名们都编进去了,连在ASCII里本来就有的数字、标点、字母都统统重新编了两个字节长的编码,这就是常说的"全角"字符,而原来在127号以下的那些就叫"半角"字符了。
-
GBK:最常用的中文码表。是在GB2312标准基础上的扩展规范,使用了双字节编码方案,共收录了21003个汉字,完全兼容GB2312标准,同时支持繁体汉字以及日韩汉字等。
-
GB18030:最新的中文码表。收录汉字70244个,采用多字节编码,每个字可以由1个、2个或4个字节组成。支持中国国内少数民族的文字,同时支持繁体汉字以及日韩汉字等。
-
-
Unicode字符集 :
-
Unicode编码系统为表达任意语言的任意字符而设计,是业界的一种标准,也称为统一码、标准万国码。
-
它最多使用4个字节的数字来表达每个字母、符号,或者文字。有三种编码方案,UTF-8、UTF-16和UTF-32。最为常用的UTF-8编码。
-
UTF-8编码,可以用来表示Unicode标准中任何字符,它是电子邮件、网页及其他存储或传送文字的应用中,优先采用的编码。互联网工程工作小组(IETF)要求所有互联网协议都必须支持UTF-8编码。所以,我们开发Web应用,也要使用UTF-8编码。它使用一至四个字节为每个字符编码,编码规则:
-
128个US-ASCII字符,只需一个字节编码。
-
拉丁文等字符,需要二个字节编码。
-
大部分常用字(含中文),使用三个字节编码。
-
其他极少使用的Unicode辅助字符,使用四字节编码。
-
-
2.3 编码引出的问题
在IDEA中,使用FileReader
读取项目中的文本文件。由于IDEA的设置,都是默认的UTF-8
编码,所以没有任何问题。但是,当读取Windows系统中创建的文本文件时,由于Windows系统的默认是GBK编码,就会出现乱码。
public class ReaderDemo { public static void main(String[] args) throws IOException { FileReader fileReader = new FileReader("E:\\File_GBK.txt"); int read; while ((read = fileReader.read()) != -1) { System.out.print((char)read); } fileReader.close(); } }
输出结果: ���
2.4 InputStreamReader类
转换流
java.io.InputStreamReader
,是Reader的子类,是从字节流到字符流的桥梁。它读取字节,并使用指定的字符集将其解码为字符。它的字符集可以由名称指定,也可以接受平台的默认字符集。
2.4.1 构造方法
方法名 | 说明 |
---|---|
InputStreamReader(InputStream in) | 创建一个使用默认字符集的字符流。 |
InputStreamReader(InputStream in, String charsetName) | 创建一个指定字符集的字符流。 |
InputStreamReader isr = new InputStreamReader(new FileInputStream("in.txt")); InputStreamReader isr2 = new InputStreamReader(new FileInputStream("in.txt") , "GBK");
2.4.2 指定编码读取文件
public class ReaderDemo2 { public static void main(String[] args) throws IOException { // 定义文件路径,文件为gbk编码 String FileName = "E:\\file_gbk.txt"; // 创建流对象,默认UTF8编码 InputStreamReader isr = new InputStreamReader(new FileInputStream(FileName)); // utf-8 // 创建流对象,指定GBK编码 InputStreamReader isr2 = new InputStreamReader(new FileInputStream(FileName) , "GBK"); // 定义变量,保存字符 int read; // 使用默认编码字符流读取,乱码 while ((read = isr.read()) != -1) { System.out.print((char)read); // ��Һ� } isr.close(); // 使用指定编码字符流读取,正常解析 while ((read = isr2.read()) != -1) { System.out.print((char)read);// 大家好 } isr2.close(); } }
2.5 OutputStreamWriter类
转换流
java.io.OutputStreamWriter
,是Writer的子类,是从字符流到字节流的桥梁。使用指定的字符集将字符编码为字节。它的字符集可以由名称指定,也可以接受平台的默认字符集
2.5.1 构造方法
方法名 | 说明 |
---|---|
OutputStreamWriter(OutputStream in) | 创建一个使用默认字符集的字符输出流。 |
OutputStreamWriter(OutputStream in, String charsetName) | 创建一个指定字符集的字符输出流。 |
OutputStreamWriter isr = new OutputStreamWriter(new FileOutputStream("out.txt")); OutputStreamWriter isr2 = new OutputStreamWriter(new FileOutputStream("out.txt") , "GBK");
2.5.2 指定编码写出数据
public class OutputDemo { public static void main(String[] args) throws IOException { // 定义文件路径 String FileName = "E:\\out.txt"; // 创建流对象,默认UTF8编码 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(FileName)); // 写出数据 osw.write("你好"); // 保存为6个字节 osw.close(); // 定义文件路径 String FileName2 = "E:\\out2.txt"; // 创建流对象,指定GBK编码 OutputStreamWriter osw2 = new OutputStreamWriter(new FileOutputStream(FileName2),"GBK"); // 写出数据 osw2.write("你好");// 保存为4个字节 osw2.close(); } }
2.6 案例:转换文件编码
2.6.1 需求
将GBK编码的文本文件,转换为UTF-8编码的文本文件。
2.6.2 需求分析
指定GBK编码的转换流,读取文本文件。
使用UTF-8编码的转换流,写出文本文件。
2.6.3 代码实现
public class TransDemo { public static void main(String[] args) { // 1.定义文件路径 String srcFile = "file_gbk.txt"; String destFile = "file_utf8.txt"; // 2.创建流对象 // 2.1 转换输入流,指定GBK编码 InputStreamReader isr = new InputStreamReader(new FileInputStream(srcFile) , "GBK"); // 2.2 转换输出流,默认utf8编码 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(destFile)); // 3.读写数据 // 3.1 定义数组 char[] cbuf = new char[1024]; // 3.2 定义长度 int len; // 3.3 循环读取 while ((len = isr.read(cbuf))!=-1) { // 循环写出 osw.write(cbuf,0,len); } // 4.释放资源 osw.close(); isr.close(); } }
3、序列化流
3.1 概述
Java 提供了一种对象序列化的机制。用一个字节序列可以表示一个对象,该字节序列包含该对象的数据
、对象的类型
和对象中存储的属性
等信息。字节序列写出到文件之后,相当于文件中持久保存了一个对象的信息。
反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化。对象的数据
、对象的类型
和对象中存储的数据
信息,都可以用来在内存中创建对象。看图理解序列化:
3.2 ObjectOutputStream类
java.io.ObjectOutputStream
类,将Java对象的原始数据类型写出到文件,实现对象的持久存储。
3.2.1 构造方法
方法名 | 说明 |
---|---|
public ObjectOutputStream(OutputStream out) | 创建一个指定OutputStream的ObjectOutputStream。 |
FileOutputStream fileOut = new FileOutputStream("employee.txt"); ObjectOutputStream out = new ObjectOutputStream(fileOut);
3.2.2 序列化常用方法
方法名 | 说明 |
---|---|
public final void writeObject (Object obj) | 将指定的对象写出。 |
3.2.3 序列化
一个对象要想序列化,必须满足两个条件:
-
该类必须实现
java.io.Serializable
接口,Serializable
是一个标记接口,不实现此接口的类将不会使任何状态序列化或反序列化,会抛出NotSerializableException
。 -
该类的所有属性必须是可序列化的。如果有一个属性不需要可序列化的,则该属性必须注明是瞬态的,使用
transient
关键字修饰。
定义类
public class Employee implements java.io.Serializable { public String name; public String address; public transient int age; // transient瞬态修饰成员,不会被序列化 public void addressCheck() { System.out.println("Address check : " + name + " -- " + address); } }
.写出对象
public class SerializeDemo{ public static void main(String [] args) { Employee e = new Employee(); e.name = "zhangsan"; e.address = "beiqinglu"; e.age = 20; try { // 创建序列化流对象 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.txt")); // 写出对象 out.writeObject(e); // 释放资源 out.close(); fileOut.close(); System.out.println("Serialized data is saved"); // 姓名,地址被序列化,年龄没有被序列化。 } catch(IOException i) { i.printStackTrace(); } } }
3.3 ObjectInputStream类
ObjectInputStream反序列化流,将之前使用ObjectOutputStream序列化的原始数据恢复为对象。
3.3.1 构造方法
方法名 | 说明 |
---|---|
public ObjectInputStream(InputStream in) | 创建一个指定InputStream的ObjectInputStream。 |
3.3.2 反序列化常用方法
如果能找到一个对象的class文件,我们可以进行反序列化操作,调用
ObjectInputStream
读取对象的方法
方法名 | 说明 |
---|---|
public final Object readObject () | 读取一个对象。 |
3.3.3 反序列化
public class DeserializeDemo { public static void main(String [] args) { Employee e = null; try { // 创建反序列化流 FileInputStream fileIn = new FileInputStream("employee.txt"); ObjectInputStream in = new ObjectInputStream(fileIn); // 读取一个对象 e = (Employee) in.readObject(); // 释放资源 in.close(); fileIn.close(); }catch(IOException i) { // 捕获其他异常 i.printStackTrace(); return; }catch(ClassNotFoundException c) { // 捕获类找不到异常 System.out.println("Employee class not found"); c.printStackTrace(); return; } // 无异常,直接打印输出 System.out.println("Name: " + e.name); // zhangsan System.out.println("Address: " + e.address); // beiqinglu System.out.println("age: " + e.age); // 0 } }
对于JVM可以反序列化对象,它必须是能够找到class文件的类。如果找不到该类的class文件,则抛出一个
ClassNotFoundException
异常。
-
注意事项
当JVM反序列化对象时,能找到class文件,但是class文件在序列化对象之后发生了修改,那么反序列化操作也会失败,抛出一个
InvalidClassException
异常。**发生这个异常的原因如下:-
该类的序列版本号与从流中读取的类描述符的版本号不匹配
-
该类包含未知数据类型
-
该类没有可访问的无参数构造方法
Serializable
接口给需要序列化的类,提供了一个序列版本号。serialVersionUID
该版本号的目的在于验证序列化的对象和对应类是否版本匹配。public class Employee implements java.io.Serializable { // 加入序列版本号 private static final long serialVersionUID = 1L; public String name; public String address; // 添加新的属性 ,重新编译, 可以反序列化,该属性赋为默认值. public int eid; public void addressCheck() { System.out.println("Address check : " + name + " -- " + address); } }
-
3.4 案例:序列化集合
3.4.1 案例需求
将存有多个自定义对象的集合序列化操作,保存到
list.txt
文件中。反序列化
list.txt
,并遍历集合,打印对象信息。
3.4.2 案例分析
1. 把若干学生对象 ,保存到集合中。 2. 把集合序列化。 3. 反序列化读取时,只需要读取一次,转换为集合类型。 4. 遍历集合,可以打印所有的学生信息
3.4.3 代码实现
public class SerTest { public static void main(String[] args) throws Exception { // 创建 学生对象 Student student = new Student("老王", "laow"); Student student2 = new Student("老张", "laoz"); Student student3 = new Student("老李", "laol"); ArrayList<Student> arrayList = new ArrayList<>(); arrayList.add(student); arrayList.add(student2); arrayList.add(student3); // 序列化操作 // serializ(arrayList); // 反序列化 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("list.txt")); // 读取对象,强转为ArrayList类型 ArrayList<Student> list = (ArrayList<Student>)ois.readObject(); for (int i = 0; i < list.size(); i++ ){ Student s = list.get(i); System.out.println(s.getName()+"--"+ s.getPwd()); } } private static void serializ(ArrayList<Student> arrayList) throws Exception { // 创建 序列化流 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("list.txt")); // 写出对象 oos.writeObject(arrayList); // 释放资源 oos.close(); } }
4、打印流
平时我们在控制台打印输出,是调用
println
方法完成的,这两个方法都来自于java.io.PrintStream
类,该类能够方便地打印各种数据类型的值,是一种便捷的输出方式。
4.1 打印流的分类
名称 | 类名 |
---|---|
字节打印流 | PrintStream |
字符打印流 | PrintWriter |
4.2 打印流的特点
- 只负责输出数据,不负责读取数据 - 永远不会抛出IOException - 有自己的特有方法
4.3 字符打印流
4.3.1 PrintWriter特点
* 自动换行 println() * 不能输出字节 可以输出字节以外的内容 * 必须是通过配置 自动刷新 (println,printf,format) boolean autoFlush: true 自动刷新 false,不自动刷新 * 包装流本身没有写出功能 * 将字节输出流转换字符输出流
4.3.2 PrintWriter构造方法
方法名 | 说明 |
---|---|
PrintWriter(String fileName) | 使用指定的文件名创建一个新的PrintWriter,而不需要自动执行刷新 |
PrintWriter(Writer out, boolean autoFlush) | 创建一个新的PrintWriter out:字符输出流 autoFlush: 一个布尔值,如果为真,则println , printf ,或format方法将刷新输出缓冲区 |
4.3.3 PrintWriter特有方法
方法名 | 说明 |
---|---|
void write(String s) | 使用指定的文件名创建一个新的PrintWriter,而不需要自动执行刷新 |
void print(String s) | 输出字符串, 没有换行 |
void println(String s) | 输出字符串并换行. 如果启动了自动刷新, 则会执行自动刷新写入数据 |
void printf(Locale l, String format, Object... args) | 使用指定格式字符串和参数将格式化的字符串写入输出流. 如果启动了自动刷新, 则会执行自动刷新写入数据 |
void format(Locale l, String format, Object... args) | 使用指定格式字符串和参数将格式化的字符串写入输出流. 如果启动了自动刷新, 则会执行自动刷新写入数据 |
4.3.4 代码演示
public class PrintWriterDemo { public static void main(String[] args) throws IOException { //PrintWriter(String fileName) :使用指定的文件名创建一个新的PrintWriter,而不需要自动执行行刷新 // PrintWriter pw = new PrintWriter("myOtherStream\\pw.txt"); // pw.write("hello"); // pw.write("\r\n"); // pw.flush(); // pw.write("world"); // pw.write("\r\n"); // pw.flush(); // pw.println("hello"); /* pw.write("hello"); pw.write("\r\n"); */ // pw.flush(); // pw.println("world"); // pw.flush(); //PrintWriter(Writer out, boolean autoFlush):创建一个新的PrintWriter PrintWriter pw = new PrintWriter(new FileWriter("myOtherStream\\pw.txt"),true); // PrintWriter pw = new PrintWriter(new FileWriter("myOtherStream\\pw.txt"),false); pw.println("hello"); /* pw.write("hello"); pw.write("\r\n"); pw.flush(); */ pw.println("world"); pw.close(); } }
4.3.5 复制Java文件打印流改进版
-
案例需求
把模块目录下的xxx.java 复制到模块目录下的 Copy.java
-
分析步骤
- 根据数据源创建字符输入流对象 - 根据目的地创建字符输出流对象 - 读写数据,复制文件 - 释放资源
-
代码实现
public class CopyJavaDemo { public static void main(String[] args) throws IOException { /* //根据数据源创建字符输入流对象 BufferedReader br = new BufferedReader(new FileReader("myOtherStream\\PrintStreamDemo.java")); //根据目的地创建字符输出流对象 BufferedWriter bw = new BufferedWriter(new FileWriter("myOtherStream\\Copy.java")); //读写数据,复制文件 String line; while ((line=br.readLine())!=null) { bw.write(line); bw.newLine(); bw.flush(); } //释放资源 bw.close(); br.close(); */ //根据数据源创建字符输入流对象 BufferedReader br = new BufferedReader(new FileReader("myOtherStream\\PrintStreamDemo.java")); //根据目的地创建字符输出流对象 PrintWriter pw = new PrintWriter(new FileWriter("myOtherStream\\Copy.java"),true); //读写数据,复制文件 String line; while ((line=br.readLine())!=null) { pw.println(line); } //释放资源 pw.close(); br.close(); } }
5、Properties集合
5.1 Properties集合概述
- 是一个Map体系的集合类 - Properties可以保存到流中或从流中加载 - 属性列表中的每个键及其对应的值都是一个字符串
5.2 Properties基本使用
public class PropertiesDemo01 { public static void main(String[] args) { //创建集合对象 // Properties<String,String> prop = new Properties<String,String>(); //错误 Properties prop = new Properties(); //存储元素 prop.put("itfxp001", "林青霞"); prop.put("itfxp002", "张曼玉"); prop.put("itfxp003", "王祖贤"); //遍历集合 Set<Object> keySet = prop.keySet(); for (Object key : keySet) { Object value = prop.get(key); System.out.println(key + "," + value); } } }
5.3 Properties作为Map集合的特有方法
方法名 | 说明 |
---|---|
Object setProperty(String key, String value) | 设置集合的键和值,都是String类型,底层调用 Hashtable方法 put |
String getProperty(String key) | 使用此属性列表中指定的键搜索属性 |
Set<String> stringPropertyNames() | 从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串 |
代码演示
public class PropertiesDemo02 { public static void main(String[] args) { //创建集合对象 Properties prop = new Properties(); //Object setProperty(String key, String value):设置集合的键和值,都是String类型,底层调用Hashtable方法put prop.setProperty("itfxp001", "林青霞"); /* Object setProperty(String key, String value) { return put(key, value); } Object put(Object key, Object value) { return map.put(key, value); } */ prop.setProperty("itfxp002", "张曼玉"); prop.setProperty("itfxp003", "王祖贤"); //String getProperty(String key):使用此属性列表中指定的键搜索属性 // System.out.println(prop.getProperty("itheima001")); // System.out.println(prop.getProperty("itheima0011")); // System.out.println(prop); //Set<String> stringPropertyNames():从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串 Set<String> names = prop.stringPropertyNames(); for (String key : names) { // System.out.println(key); String value = prop.getProperty(key); System.out.println(key + "," + value); } } }
5.4 Properties和IO流相结合的方法
方法名 | 说明 |
---|---|
void load(InputStream inStream) | 从输入字节流读取属性列表(键和元素对) |
void load(Reader reader) | 从输入字符流读取属性列表(键和元素对) |
void store(OutputStream out, String comments) | 将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法的格式写入输出字节流 |
void store(Writer writer, String comments) | 将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流 |
代码示例
public class PropertiesDemo03 { public static void main(String[] args) throws IOException { //把集合中的数据保存到文件 // myStore(); //把文件中的数据加载到集合 myLoad(); } private static void myLoad() throws IOException { Properties prop = new Properties(); //void load(Reader reader): FileReader fr = new FileReader("myOtherStream\\fw.txt"); prop.load(fr); fr.close(); System.out.println(prop); } private static void myStore() throws IOException { Properties prop = new Properties(); prop.setProperty("itfxp001","林青霞"); prop.setProperty("itfxp002","张曼玉"); prop.setProperty("itfxp003","王祖贤"); //void store(Writer writer, String comments): FileWriter fw = new FileWriter("myOtherStream\\fw.txt"); prop.store(fw,null); fw.close(); } }