java的緩衝流及使用Properties集合存取數據(遍歷,store,load)
- 2021 年 12 月 5 日
- 筆記
- JavaSE
緩衝流
概述
- 位元組緩衝流:BufferedInputStream,BufferedOutputStream
- 字元緩衝流:BufferedReader,BufferedWriter
緩衝流原理
- 緩衝區是記憶體中的一塊特定區域,與在記憶體中定義一個數組的區域不同

BufferedOutputStream
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
java.io.BufferedOutputStream
BufferedOutputStream:位元組緩衝輸出流
繼承自父類的共性成員方法:
- public void close():關閉此輸出流並釋放與此流相關聯的任何系統資源
- public void flush():刷新此輸出流並強制任何緩衝的輸出位元組被寫出
- public void write(byte[] b):將b.length位元組從指定的位元組數組寫入此輸出流
- public void write(byte[] b, int off, int len):從指定的位元組數組寫入len位元組,從偏移量off開始輸出到此輸出流
- public abstract void write(int b):將指定的位元組輸出流
構造方法:
BufferedOutputStream(OutputStream out) 創建一個新的緩衝輸出流,以將數據寫入指定的底層輸出流
BufferedOutputStream(OutputStream, int size) 創建一個新的緩衝輸出流,以將具有指定緩衝區大小的數據寫入指定的底層輸出流
參數:
OutputStream out:位元組輸出流
我們可以傳遞FileOutputStream,緩衝流會給FileOutputStream增加一個緩衝區,提高FileOutputStream的寫入效率
int size:指定緩衝流內部緩衝區的大小,不指定默認
使用步驟(重點)
1.創建FileOutputStream對象,構造方法中綁定要輸出的目的地
2.創建BufferedOutputStream對象,構造方法中傳遞FileOutputStream對象,提高FileOutputStream對象的效率
3.使用BufferedOutputStream對象中的方法write,把數據寫入到內部緩衝區中
4.使用BufferedOutputStream對象中的方法flush,把內部緩衝區中的數據,刷新到文件中
5.釋放資源(會先調用flush方法刷新數據,第四步可以省略)
*/
public class Demo01BufferedOutputStream {
public static void main(String[] args) throws IOException {
//1.創建FileOutputStream對象,構造方法中綁定要輸出的目的地
FileOutputStream fos = new FileOutputStream("D:\\environment\\java_project\\javase\\a.txt");
//2.創建BufferedOutputStream對象,構造方法中傳遞FileOutputStream對象,提高FileOutputStream對象的效率
BufferedOutputStream bos = new BufferedOutputStream(fos);
//3.使用BufferedOutputStream對象中的方法write,把數據寫入到內部緩衝區中
bos.write("我把數據寫入到內部緩衝區中".getBytes());
//4.使用BufferedOutputStream對象中的方法flush,把內部緩衝區中的數據,刷新到文件中
bos.flush();
//5.釋放資源(會先調用flush方法刷新數據,第四步可以省略)
bos.close();
}
}
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/*
java.io.BufferedInputStream extends InputStream
BufferedInputStream:位元組緩衝輸入流
繼承自父類的成員方法:
int read()從輸入流中讀取數據的下一個位元組
int read(byte[] b)從輸入流中讀取一定數量的位元組,並將其存儲在緩衝區數組b中
void close()關閉此輸入流並釋放與該流關聯的所有系統資源
構造方法:
BufferedInputStream(InputStream in) 創建一個BufferedInputStream並保存其參數,即輸入流in,以便將來使用
BufferedInputStream(InputStream in, int size) 創建具有指定緩衝區大小的BufferedInputStream並保存其參數,即輸入流in,以便將來使用
參數:
InputStream in:位元組輸入流
我們可以傳遞FileInputStream,緩衝流會給FileInputStream增加一個緩衝區,提高FileInputStream的讀取效率
int size:指定緩衝流內部緩衝區的大小,不指定默認
使用步驟(重點):
1.創建FileInputStream對象,構造方法中綁定要讀取的數據源
2.創建BufferedInputStream對象,構造方法中傳遞FileInputStream對象,提高FileInputStream對象的讀取效率
3.使用BufferedInputStream對象中的方法read,讀取文件
4.釋放資源
*/
public class Demo02BufferedInputStream {
public static void main(String[] args) throws IOException {
//1.創建FileInputStream對象,構造方法中綁定要讀取的數據源
FileInputStream fis = new FileInputStream("D:\\environment\\java_project\\javase\\a.txt");
//2.創建BufferedInputStream對象,構造方法中傳遞FileInputStream對象,提高FileInputStream對象的讀取效率
BufferedInputStream bis = new BufferedInputStream(fis);
//3.使用BufferedInputStream對象中的方法read,讀取文件
//int read()從輸入流中讀取數據的下一個位元組
/*int len = 0;//記錄每次讀取到的位元組
while ((len = bis.read())!=-1){
System.out.println(len);
}*/
//int read(byte[] b)從輸入流中讀取一定數量的位元組,並將其存儲在緩衝區數組b中
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
//4.釋放資源(只關閉緩衝流即可,關閉緩衝流會自動將位元組流關閉)
bis.close();
}
}
BufferedWriter
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/*
java.io.BufferedWriter extends Writer
BufferedWriter:字元緩衝輸出流
繼承自父類的共性成員方法:
- void write(int c) 寫入單個字元
- void write(char[] cbuf) 寫入字元數組
- abstract void write(char[] cbuf, int off, int len) 寫入字元數組的某一部分,off數組的開始索引,len寫的字元個數
- void write(String str) 寫入字元串
- void write(String str, int off, int len) 寫入字元串的某一部分,off字元串的開始索引,len寫的字元個數
- void flush() 刷新該流的緩衝
- void close() 關閉此流,但要先刷新它
構造方法:
BufferedWriter(Writer out) 創建一個使用默認大小輸出緩衝區的緩衝字元輸出流
BufferedWriter(Writer out, int sz) 創建一個使用給定大小輸出緩衝區的新緩衝字元輸出流
參數:
Writer out:字元輸出流
我們可以傳遞FileWriter,緩衝流會給FileWriter增加一個緩衝區,提高FileWriter的寫入效率
int sz:指定緩衝區的大小,不寫默認大小
特有的成員方法:
換行:void newLine() 寫入一個行分隔符,會根據不同的作業系統,獲取不同的行分隔符
換行:換行符號
windows:\r\n
linux:/n
mac:/r
使用步驟:
1.創建字元緩衝輸出流對象,構造方法中傳遞字元輸出流
2.調用字元緩衝輸出流中的方法write,把數據寫入到記憶體緩衝區中
3.調用字元緩衝輸出流中的方法flush,把記憶體緩衝區中的數據,刷新到文件中
4.釋放資源
*/
public class Demo03BufferedWriter {
public static void main(String[] args) throws IOException {
//System.out.println();其中調用的ln方法就是newLine()。
//1.創建字元緩衝輸出流對象,構造方法中傳遞字元輸出流
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\environment\\java_project\\javase\\a.txt"));
//2.調用字元緩衝輸出流中的方法write,把數據寫入到記憶體緩衝區中
for (int i = 0; i < 10; i++) {
bw.write("傳智Podcast");
//bw.write("\r\n");
bw.newLine();
}
//3.調用字元緩衝輸出流中的方法flush,把記憶體緩衝區中的數據,刷新到文件中
bw.flush();
//4.釋放資源
bw.close();
}
}
BufferedReader
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/*
java.io.BufferedReader extends Reader
繼承自父類的共性成員方法:
int read() 讀取單個字元並返回
int read(char[] cbuf) 一次讀取多個字元,將字元讀入數組
void close() 關閉該流並釋放與之關聯的所有資源
構造方法:
BufferedReader(Reader in) 創建一個使用默認大小輸入緩衝區的緩衝字元輸入流
BufferedReader(Reader in, int sz) 創建一個使用指定大小輸入緩衝區的緩衝字元輸入流
參數:
Reader in:字元輸入流
我們可以傳遞FileReader,緩衝流會給FileReader增加一個緩衝區,提高FileReader的讀取效率
特有的成員方法:
String readLine() 讀取一個文本行,讀取一行數據
行的終止符號:通過下列字元之一即可認為某行已終止:換行('\n')、回車('\r')或回車後直接跟著換行(\r\n)。
返回值:
包含該行內容的字元串,不包含任何行終止符,如果已到達流末尾,則返回null
使用步驟:
1.創建字元緩衝輸入流對象,構造方法中傳遞字元輸入流
2.使用字元緩衝輸入流對象中的方法read/readLine讀取文本
3.釋放資源
*/
public class Demo04BufferedReader {
public static void main(String[] args) throws IOException {
//1.創建字元緩衝輸入流對象,構造方法中傳遞字元輸入流
BufferedReader br = new BufferedReader(new FileReader("D:\\environment\\java_project\\javase\\a.txt"));
//2.使用字元緩衝輸入流對象中的方法read/readLine讀取文本
/*String line = br.readLine();
System.out.println(line);
line = br.readLine();
System.out.println(line);
line = br.readLine();
System.out.println(line);
line = br.readLine();
System.out.println(line);
line = br.readLine();
System.out.println(line);*/
/*
發現以上讀取是一個重複的過程,所以可以使用循環優化
不知道文件中有多少行數據,所以使用while循環
while的結束條件,讀取到null結束
*/
String line;
while ((line = br.readLine())!=null){
System.out.println(line);
}
//3.釋放資源
br.close();
}
}
使用Properties集合存儲數據,遍歷,store,load
import java.io.*;
import java.util.Properties;
import java.util.Set;
/*
java.util.Properties集合 extends Hashtable<k,v> implements Map<k,v>
Properties 類表示了一個持久的屬性集。Properties 可保存在流中或從流中載入
Properties集合是一個唯一和IO流相結合的集合
可以使用Properties集合中的方法store,把集合中的臨時數據,持久化寫入到硬碟中存儲
可以使用Properties集合中的方法load,把硬碟中保存的文件(鍵值對),讀取到集合中使用
屬性列表中每個鍵及其對應值都是一個字元串
Properties集合是一個雙列集合,key和value默認都是字元串
*/
public class Demo01Properties {
public static void main(String[] args) throws IOException {
show01();
show02();
show03();
}
/*
可以使用Properties集合中的方法load,把硬碟中(鍵值對),讀取到集合中使用
void load(InputStream inStream)
void load(Reader reader)
參數:
InputStream inStream:位元組輸入流,不能讀取含有中文的鍵值對
Reader reader:字元輸入流,能讀取含有中文的鍵值對
使用步驟:
1.創建Properties集合對象
2.使用Properties集合對象中的方法load讀取保存鍵值對的文件
3.遍歷Properties集合
注意:
1.存儲鍵值對的文件中,鍵與值默認的連接符號可以使用=,空格(或其他符號)
2.存儲鍵值對的文件中,可以使用#進行注釋,被注釋的鍵值對不會再被讀取
3.存儲鍵值對的文件中,鍵與值默認都是字元串,不用再加引號
*/
private static void show03() throws IOException{
//1.創建Properties集合對象
Properties prop = new Properties();
//2.使用Properties集合對象中的方法load讀取保存鍵值對的文件
prop.load(new FileReader("D:\\environment\\java_project\\javase\\prop.txt"));
//3.遍歷Properties集合
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}
}
/*
可以使用Properties集合中的方法store,把集合中的臨時數據,持久化寫入到硬碟中存儲
void store(OutputStream out, String comments)
void store(Writer write, String comments)
參數:
OutputStream out:位元組輸出流,不能寫中文
Writer writer:字元輸出流,可以寫中文
String comments:注釋,用來解釋說明保存的文件是做什麼用的
不能使用中文,會產生亂碼,默認是Unicode編碼
一般使用""空字元串
使用步驟:
1.創建Properties集合對象,添加數據
2.創建位元組輸出流/字元輸出流對象,構造方法中綁定要輸出的目的地
3.使用Properties集合中的方法store,把集合中的臨時數據,持久化寫入到硬碟中存儲
4.釋放資源
*/
private static void show02() throws IOException {
//1.創建Properties集合對象,添加數據
Properties prop = new Properties();
prop.setProperty("趙麗穎","168");
prop.setProperty("迪麗熱巴","165");
prop.setProperty("古力娜扎","160");
//2.創建位元組輸出流/字元輸出流對象,構造方法中綁定要輸出的目的地
FileWriter fw = new FileWriter("D:\\environment\\java_project\\javase\\prop.txt");
//3.使用Properties集合中的方法store,把集合中的臨時數據,持久化寫入到硬碟中存儲
prop.store(fw,"save data");
//4.釋放資源
fw.close();
//字元流可以寫中文,位元組流不能寫中文
//prop.store(new FileOutputStream("D:\\environment\\java_project\\javase\\prop.txt"),"");//匿名對象使用完後自動關閉
}
/*
使用Properties集合存儲數據,遍歷取出Properties集合中的數據
Properties集合是一個雙列集合,key和value默認都是字元串
Properties集合有一些操作字元串的特有方法
Object setProperty(String key, String value) 調用Hashtable的方法put
String getProperty(String key) 通過key找到value值,此方法相當於Map集合中的get(key)方法
Set<String> stringPropertyNames() 返回此屬性列表中的鍵集,其中該鍵及其對應值是字元串,此方法相當於Map集合中的keySet方法
*/
private static void show01() {
//創建Properties集合對象
Properties prop = new Properties();
//使用setProperty往集合中添加數據
prop.setProperty("趙麗穎","168");
prop.setProperty("迪麗熱巴","165");
prop.setProperty("古力娜扎","160");
//使用stringPropertyNames把Properties集合中的鍵取出,存儲到一個Set集合中
Set<String> set = prop.stringPropertyNames();
//遍歷Set集合,取出Properties集合中的每一個鍵
for (String key : set) {
//使用getProperty方法通過key獲取value
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}
}
}