Properties
表示可以从流中读取,也可以加载到流中
继承了Hashtable
1. 遍历
package com.itheima.demo07.Prop;
import java.util.Properties;
import java.util.Set;
public class Demo01Properties {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("filename", "a.txt");
properties.setProperty("length", "209385038");
properties.setProperty("location", "D:\\a.txt");
System.out.println(properties);
/*遍历打印属性集*/
Set<String> strings = properties.stringPropertyNames();
for (String s : strings) {
System.out.println(properties.getProperty(s));
}
}
}
运行结果
{location=D:\a.txt, filename=a.txt, length=209385038}
D:\a.txt
a.txt
209385038
2. 集合->硬盘 store
对应字符流中的FileWriter
,将数据从内存中写出
对应字节流中的FileOutputStream
,将数据从内存中输出
public void store(Writer writer, String comments)
throws IOException
{
store0((writer instanceof BufferedWriter)?(BufferedWriter)writer
: new BufferedWriter(writer),
comments,
false);
}
public void store(OutputStream out, String comments)
throws IOException
{
store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")),
comments,
true);
}
第一种:字符输出流,可以写中文
第二种:字节输出流,不能写中文
2.1 字符流FileWriter写文件
//使用字符流
package com.itheima.demo07.Prop;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;
public class Demo02Properties {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("filename", "a.txt");
properties.setProperty("length", "209385038");
properties.setProperty("location", "D:\\a.txt");
//创建一个字符流
FileWriter fw = new FileWriter("h.txt");
properties.store(fw, "sava date");
fw.close();
}
}
运行结果
2.2 字节流FileOutputStream从内存中输出
//使用字节流
package com.itheima.demo07.Prop;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Demo03Properties {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("filename", "a.txt");
properties.setProperty("length", "209385038");
properties.setProperty("location", "D:\\a.txt");
//创建一个字节流
FileOutputStream fos = new FileOutputStream("h.txt");
properties.store(fos, "sava date");
fos.close();
}
}
运行结果
3. 硬盘->集合 load
对应字符流中的FileRead
,将数据从内存中读出
对应字节流中的FileInputStream
,将数据从内存中输入
public synchronized void load(Reader reader) throws IOException {
load0(new LineReader(reader));
}
public synchronized void load(InputStream inStream) throws IOException {
load0(new LineReader(inStream));
}
第一种:字符输入流,可以写中文
第二种:字节输入流,不能写中文
3.1 字符流FileRead读文件
package com.itheima.demo07.Prop;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class Demo04Properties {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
FileReader fr = new FileReader("h.txt");
System.out.println(properties);
properties.load(fr);
System.out.println(properties);
Set<String> strings = properties.stringPropertyNames();
for (String s : strings) {
System.out.println(properties.getProperty(s));
}
}
}
运行结果
{}
{location=D:\a.txt, filename=a.txt, length=209385038}
D:\a.txt
a.txt
209385038
3.2 字节流FileInputStream输入到内存
package com.itheima.demo07.Prop;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class Demo05Properties {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
FileInputStream fis = new FileInputStream("h.txt");
System.out.println(properties);
properties.load(fis);
System.out.println(properties);
Set<String> strings = properties.stringPropertyNames();
for (String s : strings) {
System.out.println(properties.getProperty(s));
}
}
}
运行结果
{}
{location=D:\a.txt, filename=a.txt, length=209385038}
D:\a.txt
a.txt
209385038
tip:在文件中被连接的键值对,可以用等号,空格相连
在文件中,可以是用#进行注释
默认的键值对的读取都是字符串,不需要加""