标准I/O流
- System.in:标准输入
- System.out:标准输出
- System.in编译类型为InputStream,而运行类型为BufferedInputStream
public final static InputStream in = null;
- System.out编译类型为PrintStream,运行类型为PrintStream
public final static PrintStream out = null;
转换流
1、使用BufferedReader创建字符输入流读取文件,默认情况下按照UTF-8编码读取,但是如果文件改变编码标准后,使用字符输入流读取会产生乱码
2、字节流可以指定编码标准读取
3、转换流可以将字节流转换为字符流
4、处理纯文本数据时,如果使用字符流效率更高,并且可以有效解决中文问题,所以建议将字节流转换为字符流
- InputStreamReader
- Reader的子类
- 有方法:InputStreamReader(InputStream, Charset),使用指定编码标准进行转换
String filePath = "a.txt";
InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "gbk");
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
br.close();
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"), "gbk");
osw.write("哈哈哈");
osw.close();
打印流
- 打印流只有输出流,没有输入流
- 打印的本质就是输出,print底层就是write
- PrintStream,父类为OutputStream,不仅可以输出到屏幕,也可以输出到文件
PrintStream out = System.out;
out.print("abc".getBytes());
out.close();
System.setOut(new PrintStream("1.txt"));
System.out.println("abc");
- PrintWriter,父类为Writer,不仅可以输出到屏幕,也可以输出到文件
PrintWriter pw = new PrintWriter(System.out);
pw.print("abc");
pw = new PrintWriter(new FileWriter("1.txt"));
pw.print("abc");
pw.close();
Properties配置文件
基本介绍
- Properties为HashTable的子类
- 读取配置文件的格式必须是“键=值”
- 不需要有空格
- 值不需要引号,默认类型就是String
基本方法
- load:加载配置文件的键值对到Properties对象
- list:将数据显示到指定设备
- getProperty(key):根据键获取值
- setProperty(key, value):设置键值对到Properties对象
- store:将Properties中的键值对存储到配置文件。在IDEA中,保存信息到配置文件,如果含有中文,会存储为Unicode编码
基本应用
Properties p = new Properties();
p.load(new FileReader(1.properties));
p.list(System.out);
String user = p.getProperty();
p.setProperty("user", "lzm");
p.store(new FileOutputStream(""2.properties"), "Annotation");