package com.lufax.day20.demo01.OutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
02 06
java.io.OutputStream:此抽象类是表示输出字节流的所有类的超类
定义了一些子类共性的方法:
public void close() : 关闭此输出流并释放与此流相关联的任何系统资源。
public void flush() : 刷新此输出流并强制任何缓冲的输出字节被写出。
public void write(byte[] b) : 将b.length字节从指定的字节数组写入此输出流。
public void write(byte[] b, int off, int len) : 从指定的字节数组写入len字节,从偏移量off开始输出到此输出流
public abstract void write(int b) : 将指定的字节输出流
java.io.FileOutputStream extends OutputStream
FileOutputStream: 文件字节输出流
作用:把内存中的数据写入到硬盘的文件中
构造方法:
FileOutputStream(String name) 创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(File file) 创建一个向指定File对象表示的文件中写入数据的文件输出流。
参数:写入数据的目的地
String name:目的地是一个文件的路径
File file:目的地是一个文件
构造方法的作用:
1、创建一个FileOutputStream对象
2、会根据构造方法中传递的文件/文件路径,创建一个空的文件
3、会把FileOutputStream对象指向创建好的文件
写入数据的原理(内存-->硬盘)
java程序 --> JVM(java虚拟机) --> OS(操作系统) --> os调用写入数据的方法(系统自己的方法) --> 把数据写入到文件中
字节输出流的使用步骤(重点):
1、创建一个FileOutputStream对象,构造方法中传递写入数据的目的地
2、调用FileOutputStream对象中的write,把数据写入到文件中
3、释放资源(流使用会占用一定的内存,使用完毕要把内存清空,提高程序的效率)
*/
public class Demo01OutputStream {
public static void main(String[] args) throws IOException {
//1.创建一个FileOutputStream对象,构造方法中传递写入数据的目的地
FileOutputStream fos = new FileOutputStream("aa\\a.txt");
//2.调用FileOutputStream对象中的write,把数据写入到文件中
fos.write(97);//写入1个字节的方法
//3.释放资源(流使用会占用一定的内存,使用完毕要把内存清空,提高程序的效率)
fos.close();
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo01.OutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
/*
一次写多个字节的方法:
public void write(byte[] b) : 将b.length字节从指定的字节数组写入此输出流。
public void write(byte[] b, int off, int len) : 从指定的字节数组写入len字节,从偏移量off开始输出到此输出流
*/
public class Demo02OutputStream {
public static void main(String[] args) throws IOException {
//创建FileOutputStream对象,构造方法中绑定要写入数据的目的地
FileOutputStream fos = new FileOutputStream(new File("aa\\b.txt"));
//调用FileOutputStream对象中的方法write,把数据写入文件中
//在文件中显示100,写入字节
fos.write(49);
fos.write(48);
fos.write(48);
/*
public void write(byte[] b) : 将b.length字节从指定的字节数组写入此输出流
一次写入多个字节:
如果写入的第一个字节是正数(0-127),那么显示的时候会查询ASCII表
如果写入的第一个字节是负数,那第一个字节会和第二个字节,两个字节组成一个中文显示,查询系统默认码表(GBK)
*/
byte[] bytes = {65, 66, 67, 68, 69};//ABCDE
//byte[] bytes = {-65, -66, -67, 68, 69}; //烤紻E
fos.write(bytes);
/*
public void write(byte[] b, int off, int len) : 把字节数组的一部分写入到文件中
int off :数组的开始索引
int len: 写几个字节
*/
fos.write(bytes, 1, 2);//BC
/*
写入字符串的方法:可以使用String类中的方法把字符串转换为字节数组
byte[] getBytes() : 把字符串转换为字节数组
*/
byte[] bytes2 = "你好".getBytes();
System.out.println(Arrays.toString(bytes2));//[-28, -67, -96, -27, -91, -67]
fos.write(bytes2);
fos.close();
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo01.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
追加写/续写:使用两个参数的构造方法
FileOutputStream(String name, boolean append)创建一个向具有指定 name 的文件中写入数据的输出文件流
FileOutputStream(File file, boolean append)创建一个向指定 File 对象表示的文件中写入数据的文件输出流
参数:
String name, File file : 写入数据的目的地
boolean append:追加写开关
true:创建对象不会覆盖原文件,继续在文件的末尾追加写数据
false:创建一个新文件,覆盖源文件
写换行:写换行符号
windows : \r\n
linux : /n
mac : /r
*/
public class Demo03OutputStream {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("aa\\c.txt", true);
for (int i = 0; i < 10; i++) {
fos.write("你好".getBytes());
fos.write("\r\n".getBytes());//换行符
}
fos.close();
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo02.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
/* 12
java.io.InputStream : 字节输入流
此抽象类是表示字节输入流的所有类的超类
定义了所有子类共性的方法:
int read() 从输入流中读取数据的下一个字节
int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中。
void close() 关闭此输入流并释放与该流关联的所有系统资源
java.io.FileInputStream extends InputStream
FileInputStream:文件字节输入流
作用:把硬盘文件中的数据,读取到内存中使用。
构造方法:
FileInputStream(String name)
FileInputStream(File file)
参数:读取文件的数据源
String name : 文件的路径
File file : 文件
构造方法的作用:
1、会创建一个FileInputStream对象
2、会把FileInputStream对象指向构造方法中要读取的文件
读取数据的原理(硬盘 --> 内存)
java程序 --> JVM --> OS --> OS读取数据的方法 --> 读取文件
字节输入流的使用步骤(重点)
1、创建FileInputStream对象,构造方法中绑定要读取的数据源
2、使用FileInputStream对象中的方法read,读取文件
3、释放资源
*/
public class Demo01InputStream {
public static void main(String[] args) throws IOException {
//1、创建FileInputStream对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("aa\\a.txt");
//2、使用FileInputStream对象中的方法read,读取文件
//int read() 读取文件中的一个字节并返回该字节,读取到文件的末尾返回-1
/*int len = fis.read();
System.out.println(len);//97 a
len = fis.read();
System.out.println(len);//98 b
len = fis.read();
System.out.println(len);//99 c
len = fis.read();
System.out.println(len);//-1
len = fis.read();
System.out.println(len);//-1*/
/*
以上读取文件是一个重复的过程,所以可以使用循环优化
不知道文件中有多少个字节,使用while循环
while循环结束条件,读取到-1的时候就结束
布尔表达式(len = fis.read()) != -1
1、fis.read() :读取一个字节
2、len = fis.read() : 把读取到的字节赋值给变量len
3、(len = fis.read()) != -1 :判断变量len是否不等于-1
*/
int len = 0; //记录读取到的字节
while ((len = fis.read()) != -1) {
System.out.println((char) len);//a b c
}
//3、释放资源
fis.close();
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo02.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
/*
字节输入流一次读取多个字节的方法:
int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中
明确两件事情:
1、方法的参数byte[]的作用?
起到缓冲作用,存储每次读取到的多个字节
数组的长度一般定义为1024(1kb)或者是1024的整数倍
2、方法的返回值int是什么?
每次读取的有效字节个数
String类的构造方法
String(byte[] bytes) : 把字节数组转换为字符串
String(byte[] bytes, int offset, int length) 把字节数组的一部分转换为字符串 offset:数组的开始索引 length:转换的字节个数
*/
public class Demo02InputStream {
public static void main(String[] args) throws IOException {
//创建一个FileInputStream对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("aa\\b.txt");
//使用FileInputStream对象中的方法read读取文件
//int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲数组b 中
/*byte[] bytes = new byte[2];
int len = fis.read(bytes);
System.out.println(len);//2
System.out.println(Arrays.toString(bytes));//[65, 66]
System.out.println(new String(bytes));//AB
len = fis.read(bytes);
System.out.println(len);//2
System.out.println(Arrays.toString(bytes));//[67, 68]
System.out.println(new String(bytes));//CD
len = fis.read(bytes);
System.out.println(len);//1
System.out.println(Arrays.toString(bytes));//[69, 68]
System.out.println(new String(bytes));//ED
len = fis.read(bytes);
System.out.println(len);//-1
System.out.println(Arrays.toString(bytes));//[69, 68]
System.out.println(new String(bytes));//ED*/
/*
发现以上读取是一个重复过程,使用循环优化
不知道文件中有多少个字节,所以使用while循环
while循环结束的条件,读取到-1结束
*/
byte[] bytes = new byte[1024]; //存储读取到的多个字节
int len = 0; //记录每次读取的有效字节个数
while ((len = fis.read(bytes)) != -1) {
//String(byte[] bytes, int offset, int length) 把字节数组的一部分转换为字符串 offset:数组的开始索引 length:转换的字节个数
System.out.println(new String(bytes, 0, len)); //这里由于文件一共5个字节,我只需要前5个,不然打印得到1024长度的数组,后面全是空格
}
//释放资源
fis.close();
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo03.CopyFile;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
13
文件的复制的练习: 一读一写
明确:
数据源: c:\\1.jpg
数据的目的地: d:\\1.jpg
文件复制的步骤:
1、创建一个字节输入流对象,构造方法中绑定要读取的数据源
2、创建一个字节输出流对象,构造方法中绑定要写入的目的地
3、使用字节输入流对象中的方法read读取文件
4、使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中
5、释放资源
*/
public class Demo01CopyFile {
public static void main(String[] args) throws IOException {
//1、创建一个字节输入流对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("c:\\bk_login.jpg");
//2、创建一个字节输出流对象,构造方法中绑定要写入的目的地
FileOutputStream fos = new FileOutputStream("d:\\bk_login.jpg");
//一次读取一个字节,写入一个字节的方法
//3、使用字节输入流对象中的方法read读取文件
/* int len = 0;
while ((len = fis.read()) != -1) {
//4、使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中
fos.write(len);
}*/
//使用数组缓冲读取多个字节,写入多个字节
byte[] bytes = new byte[1024];
int len = 0;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
//5、释放资源(先关闭写的,后关闭读的,如果写完了,肯定读取完毕了)
fos.close();
fis.close();
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo04.Reader;
import java.io.FileInputStream;
import java.io.IOException;
/*
使用字节流读取中文文件, 可能会产生乱码
1个中文
GBK :占用两个字节
UTF-8:占用3个字节
*/
public class Demo01InputStream {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("aa\\c.txt");
int len = 0;
while ((len = fis.read()) != -1) {
System.out.print((char) len); //ä½ å¥½ 乱码
}
fis.close();
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo04.Reader;
import java.io.FileReader;
import java.io.IOException;
/*
java.lang.Reader : 字符输入流,是字符输入流的最顶层的父类,定义了一些共性的成员方法,是一个抽象类
共性的成员方法:
int read() 读取单个字符并返回
int read(char[] cbuf) 一次读取多个字符,将字符读入数组。
void close() 关闭该流并释放与之关联的所有资源。
java.io.FileReader extends InputStreamReader extends Reader
FileReader :文件字符输入流
作用:把硬盘文件中的数据以字符的方式读取到内存中
构造方法:
FileReader(String fileName):
FileReader(File file):
参数:读取文件的数据源
String fileName:文件的路径
File file :一个文件
FileReader构造方法的作用
1、创建一个FileReader对象
2、会把FileReader对象指向要读取的文件
FileReader字符输入流的使用步骤:
1、创建FileReader对象,构造方法中绑定要读取的数据源
2、使用FileReader对象中的方法read读取文件
3、释放资源
*/
public class Demo02Reader {
public static void main(String[] args) throws IOException {
// 1、创建FileReader对象,构造方法中绑定要读取的数据源
FileReader fr = new FileReader("aa\\c.txt");
// 2、使用FileReader对象中的方法read读取文件
//int read() 读取单个字符并返回
/*int len = 0;
while ((len = fr.read()) != -1) {
System.out.print((char) len);//你好abc123
}*/
//int read(char[] cbuf) 一次读取多个字符,将字符读入数组。
char[] cs = new char[1024];//存储读取到的多个字符
int len = 0;//记录的是每次读取的有效字符个数
while ((len = fr.read(cs)) != -1) {
/*
String类的构造方法
String(char[] value) :把字符数组转换为字符串
String(char[] value, int offset, int count) :把字符数组的一部分转换为字符串 offset:数组的开始索引 count:转换的个数
*/
System.out.println(new String(cs, 0, len));//你好abc123
}
// 3、释放资源
fr.close();
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo05.Writer;
import java.io.FileWriter;
import java.io.IOException;
/*
java.io.writer : 字符输出流,是所有字符输出流的最顶层的父类,是一个抽象类
共性的成员方法:
void write(int c) 写入单个字符
void write(char[] cbuf) 写入字符数组
abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分,off数组的开始索引,len写的字符个数
void write(String str) :写入字符串
void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
void flush()刷新该流的缓冲
void close() 关闭此流,但要先刷新它。
java.io.FileWriter extends OutputStreamWriter extends Writer
FileWriter:文件字符输出流
作用:把内存中的字符数据写入到文件中
构造方法:
FileWriter(File file) 根据给定的 File 对象构造一个 FileWriter 对象
FileWriter(String fileName) 根据给定的文件名构造一个FileWriter对象
参数:写入数据的目的地
String fileName : 文件的路径
File file :是一个文件
构造方法的作用:
1、会创建一个FileWriter对象
2、会根据构造方法中传递的文件/文件的路径,创建文件
3、会把FileWriter对象指向创建好的文件
字符输出流的使用步骤(重点)
1、创建FileWriter对象,构造方法中绑定要写入数据的目的地
2、使用FileWriter中的方法write,把数据写入到内存缓冲区中(字符转换为字节的过程)
3、使用FileWriter中的方法flush,把内存缓冲区中的数据,刷新到文件中
4、释放资源(会先把内存缓冲区中的数据刷新到文件中)
*/
public class Demo01Writer {
public static void main(String[] args) throws IOException {
// 1、创建FileWriter对象,构造方法中绑定要写入数据的目的地
FileWriter fw = new FileWriter("aa\\d.txt");
//2.使用FileWriter中的方法write,把数据写入到内存缓冲区中(字符转换为字节的过程)
//void write(int c) 写入单个字符
fw.write(97); //此时字符在内存缓冲区中,还没进入到文件汇中
//3、使用FileWriter中的方法flush,把内存缓冲区中的数据,刷新到文件中
fw.flush();
// 4、释放资源(会先把内存缓冲区中的数据刷新到文件中)
fw.close();
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo05.Writer;
import java.io.FileWriter;
import java.io.IOException;
/*
flush方法和close方法的区别
flush:刷新缓冲区,流对象可以继续使用
close:刷新缓冲区,然后通知系统释放资源,流对象不可以再被使用了。
*/
public class Demo02CloseAndFlush {
public static void main(String[] args) throws IOException {
// 1、创建FileWriter对象,构造方法中绑定要写入数据的目的地
FileWriter fw = new FileWriter("aa\\e.txt");
//2.使用FileWriter中的方法write,把数据写入到内存缓冲区中(字符转换为字节的过程)
//void write(int c) 写入单个字符
fw.write(97); //此时字符在内存缓冲区中,还没进入到文件汇中
//3、使用FileWriter中的方法flush,把内存缓冲区中的数据,刷新到文件中
fw.flush();
//刷新之后,流可以继续使用
fw.write(98);
// 4、释放资源(会先把内存缓冲区中的数据刷新到文件中)
fw.close();
//close方法之后,流已经关闭了,已经从内存中消失了。
fw.write("99");//会抛异常
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo05.Writer;
import java.io.FileWriter;
import java.io.IOException;
/*
字符输出流写入数据的其他方法
void write(char[] cbuf) 写入字符数组
abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分,off数组的开始索引,len写的字符个数
void write(String str) 写入字符串
void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
*/
public class Demo03Writer {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("aa\\f.txt");
char[] cs = {'a', 'b', 'c', 'd', 'e'};
//void write(char[] cbuf) 写入字符数组
fw.write(cs);
//abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分,off数组的开始索引,len写的字符个数
fw.write(cs, 1, 3);
//void write(String str) 写入字符串
fw.write("你好啊");
//void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
fw.write("的教案集大垃圾袋", 2, 3);
fw.close();
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo05.Writer;
import java.io.FileWriter;
import java.io.IOException;
/*
续写和换行
续写,追加写:使用两个参数的构造方法
FileWriter(String fileName, boolean append)
FileWriter(File file, boolean append)
参数:
String fileName,File file:写入数据的目的地
boolean append:续写开关 true:不会创建新的文件覆盖源文件,可以续写。false:创建新的文件覆盖源文件
换行:换行符号
windows: \r\n
linux: /n
mac : /r
*/
public class Demo04Writer {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("aa\\g.txt", true);
for (int i = 1; i <= 10; i++) {
fw.write("helloWorld" + i + "\r\n");
}
fw.close();
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo06.trycatch;
import java.io.FileWriter;
import java.io.IOException;
/*
在jdk1.7之前使用try catch finally 处理流中的异常
格式:
try{
可能会产生异常的代码
}catch{
异常的处理逻辑
} finally {
一定会执行的代码
释放资源
}
*/
public class Demo01TryCatch {
public static void main(String[] args) {
//提高变量fw的作用域,让finally可以使用
//变量在定义的时候可以没有值,但是使用的时候必须有值
//fw = new FileWriter("aa\\g.txt", true);;执行失败,fw没有值,fw没有值,fw会报错
FileWriter fw = null;
try {
fw = new FileWriter("aas\\g.txt", true);
for (int i = 1; i <= 10; i++) {
fw.write("helloWorld" + i + "\r\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 一定会执行的代码
//如果创建对象失败了,fw默认值是null,null是不能调用方法的,会抛出空指针异常,需要增加一个判断,不是null再把资源释放掉
if (fw != null) {
try {
//fw.close()方法声明抛出了IO异常对象,所以我们就得处理这个异常对象,要么throws,要么try catch
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo06.trycatch;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
JDK7的新特性
在try的后边可以增加一个(),在括号中可以定义流对象
那么这个流对象的作用域就在try中有效
try中的代码执行完毕,会自动把流对象释放,不用finally
格式:
try(定义流对象;定义流对象...){
可能产生的异常
}catch{
异常的处理逻辑
}
*/
public class Demo02JDK7 {
public static void main(String[] args) {
try ( //1、创建一个字节输入流对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("c:\\bk_login.jpg");
//2、创建一个字节输出流对象,构造方法中绑定要写入的目的地
FileOutputStream fos = new FileOutputStream("d:\\bk_login.jpg");) {
//一次读取一个字节,写入一个字节的方法
//3、使用字节输入流对象中的方法read读取文件
int len = 0;
while ((len = fis.read()) != -1) {
//4、使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中
fos.write(len);
}
} catch (IOException e) {
System.out.println(e);
}
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo06.trycatch;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
JDK9的新特性
try前边可以定义流对象
在try后边的括号中可以直接引入流对象的名称(变量名)
在try代码块执行完毕之后,流对象也可以释放掉,不用写finally
格式:
A a = new A();
B b = new B();
try(a;b){
可能产生的异常
}catch(Exception e){
异常的处理逻辑
}
*/
public class Demo03JDK9 {
public static void main(String[] args) throws FileNotFoundException {
//1、创建一个字节输入流对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("c:\\bk_login.jpg");
//2、创建一个字节输出流对象,构造方法中绑定要写入的目的地
FileOutputStream fos = new FileOutputStream("d:\\bk_login.jpg");
/* try (fis;fos) {
//一次读取一个字节,写入一个字节的方法
//3、使用字节输入流对象中的方法read读取文件
int len = 0;
while ((len = fis.read()) != -1) {
//4、使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中
fos.write(len);
}
} catch (IOException e) {
System.out.println(e);
}*/
}
}
------------------------------------------------------------------------------------
package com.lufax.day20.demo07.Prop;
import java.io.*;
import java.util.Properties;
import java.util.Set;
/*
java.util.Properties集合 extends Hashtable<K,V> implements Map<K,V>
Properties类表示了一个持久的属性值。 Properties 可保存在流中或从流中加载
Properties集合是一个唯一和IO流相结合的集合
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
属性列表中每个键及其对应值都是一个字符串。
Properties集合是一个双列集合,key和value默认都是字符串
*/
public class Demo01Properties {
public static void main(String[] args) throws IOException {
show03();
}
/*
可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
void load(InputStream inStream)
void load(Reader reader)
参数:InputStream inStream :字节输入流,不能读取含有中文的键值对
Reader reader :字符输入流,能读取含有中文的键值对
使用步骤:
1、创建Properties集合对象,添加数据
2、使用Properties集合中的方法load读取保存键值对的文件
3、遍历Properties集合
注意:
1、存储键值对的文件中,键与值默认的连接符号可以使用= ,空格(其他符号)
2、存储键值对的文件中,可以使用
3、存储键值对的文件中,键与值默认都是字符串,不再加引号
*/
private static void show03() throws IOException {
//1.创建Properties集合对象
Properties prop = new Properties();
//2、使用Properties集合中的方法load读取保存键值对的文件
prop.load(new FileReader("aa\\prop.txt")); //匿名对象使用完,可以自动释放资源,所以被手动close
//prop.load(new FileInputStream("aa\\prop.txt"));
//3、遍历Properties集合
Set<String> set = prop.stringPropertyNames();
//遍历
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key + "=" + value);
}
}
/*
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
void store(OutputStream out , String comments)
void store(Writer writer , String comments)
参数:
OutputStream out : 字节输出流,不能写入中文
Writer writer:字符输出流,可以写中文
String comments :注释,用来解释说明保存的文件是做什么用的
不能使用中文,会产生乱码,默认是unicode编码,系统默认是GBK,一般使用""空字符串
使用步骤:
1.创建Properties集合对象,添加数据
2.创建字节输出流\字符输出流对象,构造方法中绑定要输出的目的地
3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
4.释放资源
*/
private static void show02() throws IOException {
//1.创建Properties集合对象,添加数据
Properties prop = new Properties();
prop.setProperty("赵丽颖", "168");
prop.setProperty("古力娜扎", "165");
prop.setProperty("迪丽热巴", "160");
//2.创建字节输出流\字符输出流对象,构造方法中绑定要输出的目的地
FileWriter fw = new FileWriter("aa\\prop.txt");
//3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
prop.store(fw, "save data");
//4.释放资源
fw.close();
prop.store(new FileOutputStream("aa\\prop2.txt"), "save data");
}
/*
使用Properties集合存储数据,遍历取出Properties集合中的数据
Properties集合是一个双列集合,key和value默认都是字符串
Properties集合有一些操作字符串的特有方法
Object setProperty(String key, String value) 调用Hashtable的方法put
String getProperty(String key) 用指定的键在此属性列表中找到对应的value值,此方法相当于map集合中的get(key)方法
set<String> StringPropertyNames() 返回此属性列表中的键集,其中该键及其对应值是字符串,此方法相当于Map集合中的keySet方法
*/
private static void show01() {
//创建Properties集合对象
Properties prop = new Properties();
//使用setProperty添加数据
prop.setProperty("赵丽颖", "168");
prop.setProperty("古力娜扎", "168");
prop.setProperty("迪丽热巴", "168");
//prop.put(1,true);
//使用StringPropertyNames()把properties集合中的键取出,存储到一个set集合中
Set<String> set = prop.stringPropertyNames();
//遍历
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key + "=" + value);
}
}
}
------------------------------------------------------------------------------------