文章目录
IO流
1.1概述
- I 表示 Input , 是数据从硬盘文件读到内存的过程 , 即
输入
,负责读
- O表示 Output,是内存程序的数据从内存写到硬盘文件的过程,即
输出
, 负责写
IO流的分类
-
按方向:
- 输入流
- 输出流
-
按流中的数据最小单元
-
缓冲流
-
缓冲流也称为 高效流或者高级流。
-
作用: 缓冲流自带
缓冲区
、可以提高 原始字节流和字符流 读写数据的性能; -
分类:
字节缓冲流(BufferedInputStream、BufferedOutputStream)和字符缓冲流(BufferedReader、BufferedWriter)
字节缓冲流自带
8K
缓冲区
-
本文所涉及:
1.2字节流
1.2.1 FileInputStream
-
文件字节输入流 FileInputStream
-
以内存为基准,把磁盘文件中的数据以字节的形式读到内存中
构造器
-
方法 说明 public FileInputStream(File file) 创建字节输入流管道与 源文件对象
接通public FileInputStream(String pathName) 创建字节输入流管道与 源文件路径
接通
输入
方法 | 说明 |
---|---|
public int read() | 每次读取一个字节返回,如果字节已经没有可读的则返回-1 |
public int read(byte[] bytes) | 每次读取一个字节数组返回,如果字节已经没有可读的返回-1 |
public byte[] readAllBytes() throws IOException | 直接将当前字节输入流对应的 文件对象的字节数据封装到一个字节数组返回 |
每次读取一个字节
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception{
//1.先创建FileInputStream对象 字节输入流管道 与 源文件对象连接
//D:\code\javaSEImprove\file-io-app\src\abc.txt
FileInputStream is = new FileInputStream(new File("src\\abc.txt"));
//2.读取水滴 read
int rs = is.read(); //每次读取一个字节,完毕返回-1
System.out.println(rs);
System.out.println((char)rs); //转为字符型
System.out.println((char)is.read());
//3.优化, 循环 , 性能太差,而且 两者 都不能处理中文字符(2或3个字节)乱码
while((rs = is.read()) != -1){
System.out.print((char) rs);
}
is.close;
}
}
上述问题:
-
循环,性能较慢
-
读取中文字符输出 , 一定会发生乱码问题
解决中文乱码:
1.读取一个字节数组 read(byte[] bytes)
配合String解码
public class Test {
public static void main(String[] args) throws Exception{
InputStream is = new FileInputStream("src\\kk.txt");
//UTF-8 一个中文需要3个字节存储
byte[] bytes = new byte[3];
//一次访问 长度为3的字节数组
is.read(bytes);
System.out.print(new String(bytes));
//循环 访问
while(is.read(bytes) !=-1){
System.out.print(new String(bytes));
}
is.close();
}
}
还会出现 中文乱码的情况; 因为非中文字符大小还是1,字节数组内可能包含不止一个元素;
2.还是使用 read(byte[] bytes) 字节数组大小 设置 与文件大小一样
public class Test01 {
public static void main(String[] args) throws Exception{
InputStream is = new FileInputStream("src\\kk.txt");
File file = new File("src\\kk.txt");
//定义与 文件字节大小 一致的字节数组
byte[] bytes = new byte[(int)file.length()];
//
is.read(bytes);
System.out.println(new String(bytes)); //解码
//资源回收
is.close();
}
}
3.使用 readAllBytes() 获取字节数组, 再进行String解码
1.2.2 FileOutputStream
文件字节输出流 FileOutputStream 写数据
构造器
方法 | 说明 |
---|---|
public FileOutputStream(File file , boolean append) | 创建字节输出流管道与源文件对象 接通,并设置是否追加数组管道 true追加,false覆盖(默认) |
public FileOutputStream(String pathName , boolean append) | 创建字节输出流管道与源文件路径 接通,并设置是否追加数组管道 true追加,false覆盖(默认) |
输出
方法 | 说明 |
---|---|
public void write(int a) | 写一个字节出去 |
public void write(byte[] bytes) | 写一个字节数组出去 |
public void write(byte[] bytes , int pos , int len) | 写一个字节数组的一部分出去 |
-
byte[]
配合使用String编码
-
流的关闭与刷新
-
方法 说明 flush() 刷新流,还可以继续写数据 close() 关闭流,释放资源,但是在关闭之前会先刷新流。一旦关闭,就不能再写数据
-
public class FileOutputStreamDemo1 {
public static void main(String[] args) throws Exception{
//1.创建 文字字节输出流管道与目标文件接通
//可以自动创建新文件
// OutputStream os = new FileOutputStream("src/love.txt"); //每次写入,覆盖!!!
OutputStream os = new FileOutputStream("src/love.txt",true); //追加数据管道,append
os.flush();
//2.写数据出去 .write(byte)
//中文名称
os.write("马老师".getBytes()); //String编码
//换行
os.write("\r\n".getBytes()); //!!! 注意:字符串编码形式 , \r\n适应环境
//一个字节
os.write('I');
os.write(' ');
// os.write(98);
//一个字节数组
byte[] b = {'l','o','v',101};
os.write(b);
os.write(' ');
//字符串编码
os.write("you".getBytes());
os.write("\r\n".getBytes());
//截取字节数组输出
byte[] bytes = {89,'a','b',99};
os.write(bytes,0,3);
os.write("\r\n".getBytes());
//3.数据刷新,写出数据生效
os.flush(); //刷新后,可以继续输出
//4.关闭流,结束输出流管道
os.close(); //不可继续输出
}
}
1.2.3 文件拷贝
public class CopyDirTest {
public static void main(String[] args) throws Exception{
//1.创建文件字节输入流 与 源文件接通
InputStream is = new FileInputStream("src\\love.txt"); //txt
//2.创建文件字节输出流 与 目标文件接通
OutputStream os = new FileOutputStream("src\\love1.txt");
//3.转移数据
//一般,每次读入一个 字节 的桶
byte[] buffer = new byte[1024];
int len ; //读入的字节数组长度
while ((len = is.read(buffer)) != -1){
os.write(buffer,0,len);
//刷新数据
os.flush();
}
//4.关闭流
os.close();
is.close();
}
}
1.2.4 BufferedInputStream
字节缓冲输入流 BufferedInputStream
构造器
- public BufferedInputStream(InputStream is)
1.2.5 BufferedOutputStream
字节缓冲输出流 BufferedOutputStream
构造器
- public BufferedOutputStream(OutputStream os)
//文件拷贝
public class BufferedInputStreamDemo {
public static void main(String[] args) {
try (
//1.创建字节输入流管道与源文件连通
InputStream is = new FileInputStream("src/abc.txt");
//2.把原始输入流管道包装成 高级的缓冲字节输入流
InputStream bis = new BufferedInputStream(is);
//3.创建字节输出流管道与目标文件连通
OutputStream os = new FileOutputStream("src/out.txt");
//4.把原始输出流管道包装成 高级的缓冲字节输出流
OutputStream bos = new BufferedOutputStream(os);
){
int len; //记录每次读入的字节数
byte[] buffer = new byte[1024];
while ((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
}
System.out.println("复制完成了!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
1.2.6 对象序列化流
-
ObjectInputStream
和ObjectOutputStream
(对象序列化流)是Java中用于将对象持久化存储
或在网络间传输的 。 提供了对对象的序列化和反序列化功能。 -
允许将对象转换为字节流(对象序列化),然后再将字节流转化为 原始对象(反序列化);
-
ObjectOutputStream
:是一个输出流,用于将对象序列化
为字节流;使用writeObject()
方法可以将对象写入到输出流中。当需要将一个对象进行持久化存储或在网络上进行传输时,可以使用 ObjectOutputStream将其序列化为字节流; -
ObjectInputStream
:是一个输入流,用于从字节流中反序列化
对象;使用readOject()
方法可以从输入流中读取字节流 并将其转换为原始对象。当需要从持久化存储或网络中接受的字节流中恢复对象时,可以使用ObjectInputStream
进行反序列化;
对象序列化流的工作原理:是将对象以二进制
形式序列化,并将序列化数据写入流中,以便在需要时重新构建该对象。这使得对象能够跨越应用程序、操作系统和网络传输,而无需重新编写复杂的数据映射逻辑。
使用对象序列化流的注意:
-
类的序列化:要将对象进行序列化,相应的类必须实现
Serializable
接口,这是一个标记接口,没有定义任何方法,仅用于表示该类可以被序列化。 -
字段的序列化:要将字段序列化为字节流,它们必须是可序列化的。 可序列化的字段是指原始类型、String、数组和实现了Serializable接口的对象。
-
transient
修饰符:使用transient
修饰字段时,这些字段将不会被序列化,自然它们在反序列化过程中保持 默认值 -
版本控制:当序列化类发生更改时,可能需要进行版本控制以确保兼容性。可以通过在类中定义一个名为
serialVersionUID
的静态常量来实现版本控制。
什么是Java序列化?何时需要序列化
Java序列化是将Java对象转换为字节序列的过程,反序列化是将字节序列转换成Java对象的过程。
Java序列化以便可以将其存储在文件中、在网络上传输或者在进程之间传递,可以将对象的状态保存到硬盘或者内存中,同时又能够方便地将对象恢复到其原始状态。
1.需要序列化的情况通常包括:
将对象存储到文件中,以便在以后的某个时间点检索它;
将对象通过网络传输,例如在客户端和服务器之间传输数据;
在进程之间传递对象。
2.需要注意的是,不是所有的对象都需要或能够序列化。例如下面这些情况:
包含非序列化字段的类对象,比如 Thread 类、InputStream 类等;
包含对不可序列化实例化对象的引用的类对象,因为这个引用指向的对象不可序列化,那么这个类对象也不可序列化;
包含循环引用的类对象,即一个对象引用了另一个对象,而后者又引用了前者,这种情况会导致无限递归,无法序列化;
包含被 static修饰的静态变量的类对象,因为静态变量不属于任何一个实例化对象,无法通过实例化对象进行序列化;
包含匿名内部类、局部内部类和 lambda 表达式的类对象,因为这些类没有名字,无法被序列化;
包含被 transient 修饰的变量的类对象,因为 transient 可以用来修饰不需要序列化的变量,这些变量的值在序列化和反序列化时会被忽略。
//Student类序列化
public class Student implements Serializable {
//申明序列化的版本号
//序列化的版本号与反序列化的版本号必须一致,否则报错! 需要重新输出 序列化,才能反序列化
public static final long serialVersionUID = 2;
private String name;
private char sex;
private int age;
private String loginName;
//transient修饰的成员变量不参与序列化
private transient String passWord;
public Student(){
}
public Student(String name, char sex, int age, String loginName, String passWord) {
this.name = name;
this.sex = sex;
this.age = age;
this.loginName = loginName;
this.passWord = passWord;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex=" + sex +
", age=" + age +
", loginName='" + loginName + '\'' +
", passWord='" + passWord + '\'' +
'}';
}
}
public class ObjectOutputStreamDemo {
public static void main(String[] args) {
//对象序列化存储
try(//1.创建字节输入流管道与源文件接通
OutputStream os = new FileOutputStream("src/out03.txt");
//2.对象序列化:使用对象字节输出流包装输出流管道
ObjectOutputStream oos = new ObjectOutputStream(os);
){
Student student = new Student("马老师", '男', 22,"Mr.Ma","123456");
//3.直接调用序列化方法
oos.writeObject(student);
}catch (Exception e){
e.printStackTrace();
}
}
}
public class ObjectInputStreamDemo1 {
public static void main(String[] args) throws Exception{
//1.创建对象字节输入流管道包装 低级字节输入流管道
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/out03.txt"));
//2.调用对象反序列化方法
Student s = (Student) ois.readObject(); //反序列化输出
//transient 密码不被反序列化
System.out.println(s);
}
}
结果
Student{name='马老师', sex=男, age=22, loginName='Mr.Ma', passWord='null'}
1.2.7 打印流
输入
- 打印流可以实现方便、高效的打印数据到
文件
中去。 - 打印流一般是指:
PrintStream
、PrintWriter
两个类 - 可以实现打印什么数据就是什么数据,例如打印整数97就是97,打印boolean的true,写出去就是true
构造器
构造器 | 说明 |
---|---|
public PrintStream(OutputStream os) | 打印流直接通向字节输出流管道 |
public PrintStream(File f) | 打印流直接通向文件对象 |
public PrintStream(String filePath) | 打印流直接通向文件路径 |
方法
- public void print(Xxx xx) 打印任意类型的数据出去
public class PrintStreamDemo {
public static void main(String[] args) throws Exception{
//1.创建打印流对象
PrintStream ps = new PrintStream(new FileOutputStream("src/out04.txt"));
// PrintWriter pw = new PrintWriter("src/out04.txt"); //两者使用一样
ps.println(98);
ps.println('a');
ps.println("我爱你中国");
ps.print(true);
ps.println();
ps.close();
}
}
- 输出语句 重定向
- 属于打印流的一种应用,可以把输出语句的打印位置 改为 文件
public class PrintStreamDemo2 {
public static void main(String[] args) throws Exception{
// public final static PrintStream out = null;
System.out.println("锦瑟无端五十弦");
System.out.println("一弦一柱思华年");
//定义打印流
PrintStream ps = new PrintStream(new FileOutputStream("src/out04.txt",true));
//重定向打印 格式固定!!!
System.setOut(ps);
//此时转为 打印到ps , 而不是控制台
System.out.println("庄生晓梦迷蝴蝶");
System.out.println("望帝春心归杜鹃");
}
}
1.3字符流
1.3.1 FileReader
-
文件字符输入流:Reader
-
作用:以内存为基准,把磁盘文件中的数据以 字符 的形式读到内存中去
构造器
构造器 | 描述 |
---|---|
public FileReader(File file) | 创建字符 输入流管道 与 源文件对象接通 |
public FileReader(String pathname) | 创建字符 输入流管道 与 源文件路径接通 |
输入
方法 | 说明 |
---|---|
public int read() | 每次读取一个字符返回,如果字符已经没有可读的返回 -1 |
public int read(char[] buffer) | 每次读取一个字符数组,返回读取的字符个数,如果字符已经没有可读的返回-1 |
public class FileReaderDemo {
public static void main(String[] args) throws Exception{
//1.创建构造器
//创建输入流管道与源文件对象连通
Reader r = new FileReader("src/love.txt");
int rs;
//2.read() 输入
while ((rs=r.read())!=-1){
System.out.print((char)rs);
}
//3.read(byte[] buffer)
char[] buffer = new char[1024];
while ((rs=r.read(buffer))!=-1){
System.out.print(rs);
}
}
}
- 读取中文字符不会出现乱码 (如果代码和文件的编码一致)
- 性能较慢
1.3.2 FileWriter
-
文件字符输出流:FileWriter
-
作用:以内存为基准,把内存中的数据以字符的形式
写
出到磁盘文件去的流;
构造器
构造器 | 说明 |
---|---|
public FileWriter(File file , boolean append) | 创建字符输出流管道与源文件对象接通 , 是否追加数据 |
public FileWriter(String filePath , boolean append) | 创建字符输出流管道与源文件对象接通,是否追加数据 |
输出
方法 | 描述 |
---|---|
void write(char n) | 写一个字符 |
void write(char[] cbuf) | 写入一个字符数组 |
void write(char[] cbuf, int off , int len) | 写入字符数组的一部分 |
void write(String str) | 写一个字符串 |
void write(String str, int off , int len) | 写一个字符串的一部分 |
void write(int n) | 写一个字符 |
流的关闭与刷新
方法 | 描述 |
---|---|
flush() | 刷新流,还可以继续写数据 |
close() | 关闭流,释放资源,但是在关闭之前会先刷新流。一旦关闭,就不能再写数据 |
public class FileWriterDemo {
public static void main(String[] args) throws Exception{
//1.创建 字节输出管道与文件连通
// Writer wr = new FileWriter("src/out01.txt"); 覆盖
Writer wr = new FileWriter("src/out01.txt",true); // 追加
//2.write() 输出
//单个字符
wr.write(97);
wr.write('b');
wr.write('马');
wr.write("\r\n");
//一个字符串
wr.write("马甜甜");
wr.write("马老师喜欢马老师",0,5);
wr.write("\r\n");
//一个字符数组
char[] chars = "马老师是中国人".toCharArray(); //字符串转换为 一个字符数组
wr.write(chars);
wr.write("\r\n");
wr.write(chars,0,6);
//wr.write("\r\n"); 有点意外
wr.flush();
wr.close();
}
}
1.3.3 BufferedReader
字符缓冲输入流 BufferedReader
- 除了提高字符输入流 读取速度外,还多了 按行读取数据的功能
构造器
- public BufferedReader(Read r) 可以把低级的字符输入流包装成一个高级的缓冲字符流管道,从而提高字符输入流读取数据性能
新增功能
- public
String readLine()
读取一行数据返回,无行可读返回NULL
public class BufferedReaderDemo {
public static void main(String[] args) {
try (
Reader fr = new FileReader("src/abc.txt");
BufferedReader br = new BufferedReader(fr);
)
{
String line ;
//BufferedReader 独有方法readLine() 读取一整行字符
while ((line = br.readLine()) != null){
//需要添加换行符 !!!
System.out.println(line);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
1.3.4 BufferedWriter
字符缓冲输出流 BufferedWriter
- 除了提高字符输出流 写取 数据的性能外, 多了
换行
的功能 ; 之前是 “\r\n”
构造器
- public BufferedWriter(Writer w)
新增功能
- public void
newLine()
换行操作
public class BufferedWriterDemo {
public static void main(String[] args) throws Exception{
//1.创建 字节输出管道与文件连通
// Writer wr = new FileWriter("src/out01.txt"); 覆盖
Writer fw = new FileWriter("src/out01.txt",true); // 追加
//shift + F6 全改变量名
BufferedWriter bw = new BufferedWriter(fw);
//2.write() 输出
//单个字符
bw.write(97);
bw.write('b');
bw.write('马');
bw.write("\r\n");
//独有换行方法
bw.newLine();
//一个字符串
bw.write("马甜甜");
bw.write("马老师喜欢马老师",0,5);
bw.write("\r\n");
//一个字符数组
char[] chars = "马老师是中国人".toCharArray();
bw.write(chars);
bw.write("\r\n");
bw.write(chars,0,6);
//wr.write("\r\n"); 有点意外
bw.flush();
bw.close();
}
}
综合案例
出师表有序拷贝
public class BufferedTest {
public static void main(String[] args) {
//出师表有序拷贝
try(
//1.创建输入缓冲流
BufferedReader br = new BufferedReader(new FileReader("src/csb.txt"));
//2.创建输出缓冲流
BufferedWriter bw = new BufferedWriter(new FileWriter("src/csbSort.txt"));
){
//3.创建复制 文件内容的容器
List<String> out = new ArrayList<>();
//4.换行 复制
String line;
while ((line = br.readLine()) != null){
out.add(line);
}
//5.定义序列集合
List<String> index = new ArrayList<>();
Collections.addAll(index,"一","二","三","四","五","六","七","八","九","十");
//6.排序
//自定义排序规则
// Collections.sort(out, (o1, o2) -> {
// //按照字符串首字母,所在 index集合中的 序列进行比较
// //indexOf() 获取元素的序列, substring截取字符串
// return index.indexOf(o1.substring(0,o1.indexOf("、"))) - index.indexOf(o2.substring(0,o2.indexOf("、")));
// });
Collections.sort(out, Comparator.comparingInt(o -> index.indexOf(o.substring(0, o.indexOf("、")))));
//7.遍历排序后的集合
for (String s : out) {
bw.write(s);
bw.newLine();
bw.flush();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
结果
1.3.5 转换流
- 字符输入转换流,
InputStreamReader
,可以把字节输入流
按照 指定编码转换成字符输入流
;- 构造器
- public InputStream(InputStream is) 把字节输入流转换成字符输入流。与默认的
FileReader
一样,几乎不用; - public InputStream(InputStream is , String charset) 把字节输入流按照
指定编码
转换成字符输入流,这样字符流中字符就不乱码了 (重点
)
- public InputStream(InputStream is) 把字节输入流转换成字符输入流。与默认的
- 构造器
public class InputStreamReaderDemo {
public static void main(String[] args) {
try (
//处理中文乱码问题
//创建原始字节流
InputStream is = new FileInputStream("src\\out01.txt");
//多态,字符转换流 -> 父类 Reader
Reader isr = new InputStreamReader(is,"UTF-8"); //要与文件编码格式一致
//创建 字符缓冲流
BufferedReader br = new BufferedReader(isr);
){
String line ;
//BufferedReader 独有方法readLine() 读取一整行字符
while ((line = br.readLine()) != null){
//需要添加换行符
System.out.println(line);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
- 字符输出转换流,
OutputStreamWriter
,可以把字节输出流
按照指定编码转换成字符输出流
;- 构造器
- public OutputStreamWriter(OutputStream os) 可以把字节输出流 按照代码默认编码转换成 字符输出流。几乎不用
- public OutputStreamWriter(OutputStream os ,String charset)可以把字节输出流 按照
指定编码
转换成 字符输出流(重点
)
- 构造器
public class OutputStreamWriterDemo {
public static void main(String[] args) {
try(
OutputStream os = new FileOutputStream("src/out02.txt");
Writer osw = new OutputStreamWriter(os,"UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
){
bw.write("我爱你中国~~~");
bw.newLine();
bw.write("我爱你中国~~~");
bw.write("我爱你中国~~~");
bw.write("我爱你中国~~~");
}catch (Exception e){
e.printStackTrace();
}
}
}
1.4资源释放的方式
1.try-catch-finally
finally
:在异常处理时提供 finally 块来执行所有的清除操作,比如:IO流中的释放资源- 特点:被finally控制的语句 最终一定会执行,除非JVM退出
System.exit(0)
; - 异常处理标准格式: try…catch…finally
- 手动释放资源
public class TryCatchFinallyTest {
public static void main(String[] args) throws Exception{
File sourceFile = new File("src\\love.txt");
File targetFile = new File("src\\love1.txt");
//检查文件是否存在 , 创建源文件并利用 Files类写入内容(File类不能写入内容)
if (!sourceFile.exists()){
try{
sourceFile.createNewFile();
Files.write(sourceFile.toPath(),"你好,中国".getBytes(StandardCharsets.UTF_8));
}catch (IOException e){
e.printStackTrace();
}
}
//文件输入输出流
InputStream is = new FileInputStream(sourceFile);
OutputStream os = new FileOutputStream(targetFile,true);
byte[] bytes = new byte[1024];
//try-catch-finally
try {
int len;
while ((len = is.read(bytes))!=-1){
os.write(bytes,0,len);
//刷新
os.flush();
}
}catch (Exception e){
e.printStackTrace();
}finally{
is.close();
os.close();
}
}
}
2.try(定义流对象)-catch
- JDK7 没有finally
- 资源用完最终自动释放
try(定义流对象){
可能出现异常的代码;
}catch(异常类名 变量名){
异常的处理代码;
}
3.catch(输入输出流对象)-catch
-
JDK9
-
资源用完最终自动释放
定义输入流对象;
定义输出流对象;
try(输入流对象;
输出流对象;){
可能出现异常的代码;
}catch(异常类名 变量名){
异常的处理代码;
}
1.5 Properties
- Properties属性集对象
- Properties代表的是一个属性文件,可以把自己对象中的键值对信息存到一个属性文件中去
- 属性文件:后缀是 . properties结尾的文件,里面的内容都是 key = value ,用作系统配置信息
集合方法
- 添加 put (
独有
):public ObjectsetProperty
(String key ,String value) - 此属性列表中指定索引 属性值 get (
独有
) :public StringgetProperty
(String key) - 获取集合 所有键的名称集合 keySet (
独有
): public Set< String > stringPropertyNames()
Properties与IO流 结合 的方法
- 读取
- void load(InputStream is) 从输入字节流读取属性列表(properties)
- void load(Reader r) 从输入字符流读取属性列表
- 存储
- void store(OutputStream out , String comments) 将此属性列表
写入
到 属性文件中 - void store(Writer writer , String comments) 将此属性列表 写入到 属性文件中
- void store(OutputStream out , String comments) 将此属性列表
public class PropertiesDemo {
public static void main(String[] args) throws Exception{
//1.创建 空的构造器
//使用Properties 把键值对信息存入到属性文件中
Properties properties = new Properties();
// properties.put("msl","456"); 父类方法
//独有方法 setProperty 添加
properties.setProperty("Mr.Ma","123456");
properties.setProperty("Mr.Li","1234");
properties.setProperty("Ms.Wan","13456");
properties.setProperty("MsMa","1256");
System.out.println(properties);
//2.保存管道,存储properties
/**
参数一:保存管道 字符(字节)输出流管道 , 最好保存至 properties文件
参数二:保存心得(注释)
*/
properties.store(new FileWriter("src/out04.properties",true),
"This is a good day~~~");
}
}
public class PropertiesDemo2 {
public static void main(String[] args) throws Exception{
//1.Properties 读取属性文件中的键值对信息
Properties properties = new Properties();
System.out.println(properties); // {}
//2.加载 属性文件中 键值对数据到属性对象Properties中去
properties.load(new FileReader("src/out04.properties"));
System.out.println(properties); //{MsMa=1256, Ms.Wan=13456, Mr.Ma=123456, Mr.Li=1234}
//3.读取 键对应值
//独有方法 getProperty
String s = properties.getProperty("MsMa");
System.out.println(s);
//4.获取所有键的名称,返回Set集合
Set<String> names = properties.stringPropertyNames();
for (String name : names) {
System.out.println(name);
}
}
}
总结
- 字节流适合做一切文件数据的拷贝(音视频、文本)
- 字节流 不适合 读取中文内容输出
- 字符流适合做文本文件的输出(读、写)