字符流
字符输入流
FileReader
构造方法
new FileReader(File file)
new FileReader(String s)
常用方法
close() 关流
read() 读取一个字符
返回值:读取到的字符的数值。比如读到a 返回的是97
read(char[] chs) 读取一个字符数组的内容
返回值:实际读取到的字符个数
字符输出流
FileWriter
构造方法
new FileWriter(File file);
new FileWriter(String s);
new FileWriter(File file,boolean b);
new FileWriter(String s,boolean b);
常用方法
close() 关流
flush() 刷新 (目前为止只有字符输出流需要刷新)
write(int c) 写出一个字符
write(char[] chs) 写出一个字符数组的内容
write(char[] chs,int off,int len)
写出数组的一部分
write(String s) 写出一个字符串
Properties
其实是一个集合,但是可以做和IO相关的操作
集合方法
setProperty(键,值) 添加兼职对
String 值 = getProperty(键) 根据键获取值
getPropertyNames() 获取所有的键
和IO相关的方法
load(输入流) 把文件中的键值对读取到集合中
store(输出流,注释内容) 把集合中的内容写出到文件中
ResourceBundle
作用:
更方便的把文件中的内容读取到内存中。
和Properties中load()方法的作用是一样的。
前提:
1.文件必须在src下
2.文件必须以.properties结尾
读取方式:
//获取对象
//不需要写清楚文件路径,因为已经规定了文件的路径
//不许要写清楚文件后缀名,已经规定了文件后缀名
ResourceBundle bundle = ResourceBundle.getBundle("123");
//根据键获取值
String s = bundle.getString("abc");
System.out.println(s)
ResourceBundle其实Properties的load方法功能是一样的。
ResourceBundle可以不用。