IO流-4
1. 高效缓冲流
1.1 概述
对硬盘进行数据的读取相比于从内存中存取数据要慢的多。所以JDK为我们提供了高效缓冲流来提高我们IO流的效率。内部原理就是借助内存的缓冲区来减少硬盘IO的次数,提高性能。
1.2 分类
字节流
输入流
BufferedInputStream
输出流
BufferedOutputStream字符流
输入流
BufferedReader
输出流
BufferedWriter
1.2 对象的创建
构造方法:
public BufferedInputStream(InputStream in)public BufferedOutputStream(OutputStream out)public BufferedReader(Reader in)public BufferedWriter(Writer out)
范例:
public static void main(String[] args) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\root\\Desktop\\test\\汉字.txt"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\root\\Desktop\\test\\汉字3.txt"));BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\root\\Desktop\\test\\汉字3.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\root\\Desktop\\test\\汉字3.txt"));}
1.3 特有的方法
| 类 | 方法 | 作用 |
|---|---|---|
| BufferedReader | public String readLine() throws IOException | 一次读取一行数据,读到的内容不包含换行符,读到了文件末尾返回null。 |
| BufferedWriter | public void newLine() throws IOException | 写入一个换行符,会根据系统变化 |
范例:
public static void main(String[] args) throws IOException {//readLine//创建对象BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\root\\Desktop\\test\\333.txt"));//读取数据String line;while((line=br.readLine())!=null){//把读到的内容输出System.out.println(line);}//释放资源br.close();}
public static void main(String[] args) throws IOException {//newLine//创建对象BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\root\\Desktop\\test\\444.txt"));//写入数据bw.write("你好啊");//写入换号符bw.newLine();bw.write("我很好");//释放资源bw.close();}
public static void main(String[] args) throws IOException {//newLine//创建对象BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\root\\Desktop\\test\\333.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\root\\Desktop\\test\\444.txt"));//循环读写数据 把读到的数据写入目标文件中String line;while((line=br.readLine())!=null){//把读到的数据写入文件bw.write(line);//写入换行符bw.newLine();}//释放资源br.close();bw.close();}
1.4 应用场景
如果想让IO操作效率更高或者想使用特有的方法(readLine、newLine)就可以使用高效缓冲流。
2. 转换流
2.1 概述
如果我们需要把字节流转换成字符流,可以使用转换流来实现转换。
2.2 分类
| 流类型 | 类 |
|---|---|
| 输入流 | InputStreamReader |
| 输出流 | OutputStreamWriter |
2.2 转换流的使用
当我们需要把字节流转换成字符流时直接使用转换流的构造方法进行转换即可。
public InputStreamReader(InputStream in)public InputStreamReader(InputStream in, String charsetName)public OutputStreamWriter(OutputStream out)public OutputStreamWriter(OutputStream out, String charsetName)
范例:
public static void main(String[] args) throws FileNotFoundException {//字节流对象FileInputStream fis = new FileInputStream("C:\\Users\\root\\Desktop\\test\\汉字.txt");FileOutputStream fos = new FileOutputStream("C:\\Users\\root\\Desktop\\test\\转换流测试.txt");//转换成字符流InputStreamReader isr = new InputStreamReader(fis);OutputStreamWriter osw = new OutputStreamWriter(fos);}
3. Properties
Properties 其实是一个Map集合,其Key和Value都是String类型。他提供了和流结合的方法,可以方便我们把集合中的数据写入文件或者是把文件中的数据读取到集合中。
3.1 对象创建
构造方法:
public Properties()
范例:
public static void main(String[] args) {Properties properties = new Properties();}
3.2 常用方法
Map集合的方法Properties都有,这里不做演示。因为我们在使用Properties的时候一般都是使用其特有的一些方法。
常用方法:
public synchronized Object setProperty(String key, String value) //设置键值对public String getProperty(String key) //根据键获取对应的值public Set<String> stringPropertyNames()//获取所有Key的集合
范例:
public static void main(String[] args) {Properties properties = new Properties();//存储key valueproperties.setProperty("name","三更");properties.setProperty("age","17");//获取对应的值String v = properties.getProperty("name");Set<String> keys = properties.stringPropertyNames();for (String key : keys) {String value = properties.getProperty(key);System.out.println(key+"===="+value);}}
3.2 和流结合的方法
public synchronized void load(Reader reader) throws IOException //通过字符流加载数据public synchronized void load(InputStream inStream) throws IOException //通过字节流加载数据public void store(Writer writer, String comments) throws IOException //通过字符流保存数据public void store(OutputStream out, String comments) throws IOException//通过字节流保存数据
范例:
public static void main(String[] args) throws IOException {Properties pro = new Properties();//存数据pro.setProperty("name","三更");pro.setProperty("age","16");//把集合中的数据写入文件FileWriter fw = new FileWriter("C:\\Users\\root\\Desktop\\test\\proTest.txt");pro.store(fw,"java");fw.close();}
public static void main(String[] args) throws IOException {//创建集合Properties pro = new Properties();//从文件中加载数据FileReader fr = new FileReader("C:\\Users\\root\\Desktop\\test\\proTest.txt");pro.load(fr);}
3.3 应用场景
Properties主要是用来读取和写入配置文件时使用。要求配置文件中的数据格式是: key=value
