字符流代码演示和Properties的使用演示
1.使用字符流读取中文文件
- java.io.Reader 字符输入流
- java.io.FileReader extends InputStreamReader extends Reader
- 把硬盘的文件数组以字符形式读取到内存
public class Demo1 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("d://a.txt");
// int len = 0;
// while ((len = fr.read())!=-1){
// System.out.print((char) len);
// }
char[] cs = new char[1024];
int len = 0;
while ((len=fr.read(cs))!=-1){
System.out.println(new String(cs,0,len));
}
fr.close();
}
}
2.使用字符流输出到文件
- FileWriter extends OutputStreamWriter extends Writer
- 把内存中的字符数据写入到文件中
- 1.创建FileWriter对象,构造方法中绑定要写入数据的目的地
- 2.使用FileWriter中的方法write,把数据写入到内存缓冲区(字符转换为字节的过程)
- 3.使用filewriter中的方法flush,把内存缓冲区中的数据,刷新到文件里
- 4.释放资源(会先把内存缓冲区中的数据刷新到文件里)
public class Demo1 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("d://c.txt");
fw.write(97);
fw.flush();
fw.close();
}
}
- flush和close的区别
flush,刷新缓冲区,流可继续使用
close 先刷新缓冲区,但流不可再使用
public class Demo2 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("d://d.txt");
fw.write(97);
fw.flush();
fw.write(98);
fw.close();
//fw.write(99); 错误操作
}
}
public class Demo3 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("d://e.txt");
char[] cs = {'a','b','c','d','e'};
fw.write(cs);
fw.flush();
fw.write(cs,1,3);
fw.flush();
fw.write("传智播客");
fw.flush();
fw.close();
}
}
续写和换行
public class Demo4 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("d://f.txt",true);
for (int i = 0; i < 10; i++) {
fw.write("Hello world"+i);
fw.write("\r\n");
}
fw.close();
}
}
3.Properties的使用
/**
* Properties表示一个持久的属性集。可从流中保存或加载
* 唯一一个与IO流相结合的集合
* 方法store,把集合中的临时数据,写入到硬盘中存储
* 方法load,把硬盘中保存的文件(键值对)读取到集合中使用
* properties是一个双列集合。key和value默认都是字符串
*/
public class Demo1 {
public static void main(String[] args) throws IOException{
//show01();
//show2();
show3();
}
/*
使用properties集合存储数据,遍历取出其中的数据
Object setProperty(String key,String value)
String getProperty(String key) 类似Map的get(key)方法
Set<String> stringPropertyNames() 返回属性列表中的键集。相当于Map中的KeySet()方法
*/
private static void show01() {
Properties prop = new Properties();
prop.setProperty("james","206");
prop.setProperty("kobe","198");
prop.setProperty("Wade","193");
//获取键
Set<String> set = prop.stringPropertyNames();
for(String key:set) {
String value = prop.getProperty(key);
System.out.println(value);
}
}
/**
* void store(OutputStream out,String comments) 传入字节流
* void store(writer writer,String comments) 传入字符流
* comments是注释。不能使用中文,默认是Unicode编码
*/
private static void show2() throws IOException {
//1.创建Properties对象,添加数据
Properties prop = new Properties();
prop.setProperty("james","206");
prop.setProperty("kobe","198");
prop.setProperty("Wade","193");
//2.创建字节/字符输出流
FileWriter fw = new FileWriter("d://prop.txt");
//3.
prop.store(fw,"save data");
fw.close();
}
/*
load方法
把键值对文件读取到集合使用
load() 字节
load() 字符
*/
private static void show3() throws IOException{
Properties prop = new Properties();
FileReader fr = new FileReader("d://prop.txt");
prop.load(fr);
Set<String> keySet = prop.stringPropertyNames();
for(String key : keySet) {
System.out.println(key+prop.getProperty(key));
}
}
}