緩沖流
緩沖流概述
緩沖流介紹:
緩沖流也稱為高效流、或者高級流。之前學(xué)習(xí)的字節(jié)流和字符流可以稱為原始流。
作用:緩沖流自帶緩沖區(qū)、可以提高原始字節(jié)流、字符流讀寫數(shù)據(jù)的性能
緩沖流分為: 字節(jié)緩存輸入流、字節(jié)緩沖輸出流、字符緩沖輸入流、字符緩沖輸出流
字節(jié)緩沖流
字節(jié)緩沖輸入流:BufferedInputStream,提高字節(jié)輸入流讀取數(shù)據(jù)的性能,讀寫功能上與原始流相比并無變化。
字節(jié)緩沖輸出流:BufferedOutputStream,提高字節(jié)輸出流讀取數(shù)據(jù)的性能,讀寫功能上與原始流相比并無變化。
字節(jié)緩沖流性能優(yōu)化原理:
字節(jié)緩沖輸入流自帶了8KB緩沖池,以后我們直接從緩沖池讀取數(shù)據(jù),所以性能較好。
字節(jié)緩沖輸出流自帶了8KB緩沖池,數(shù)據(jù)就直接寫入到緩沖池中去,寫數(shù)據(jù)性能極高了。
將原始流包裝為緩沖流的構(gòu)造器如下:
構(gòu)造器 | 說明 |
---|---|
BufferedInputStream(InputStream is) | 可以把低級的字節(jié)輸入流包裝成一個高級的緩沖字節(jié)輸入流管道,從而提高字節(jié)輸入流讀數(shù)據(jù)的性能 |
BufferedOutputStream(OutputStream os) | 可以把低級的字節(jié)輸出流包裝成一個高級的緩沖字節(jié)輸出流,從而提高寫數(shù)據(jù)的性能 |
public static void main(String[] args) throws Exception {
// 創(chuàng)建原始字節(jié)輸入流
InputStream is = new FileInputStream("/Users/chenyq/Documents/test.txt");
// 創(chuàng)建原始字節(jié)輸出流
OutputStream os = new FileOutputStream("/Users/chenyq/Documents/test1.txt");
// 將低級的字節(jié)輸入流包裝為高級的緩存輸入流
InputStream bis = new BufferedInputStream(is);
// 將低級的字節(jié)輸出流包裝為高級的緩存輸出流
OutputStream bos = new BufferedOutputStream(os);
}
讀寫功能和之前原始流的讀寫并無區(qū)別:
public static void main(String[] args) {
try (
// 創(chuàng)建原始字節(jié)輸入流
InputStream is = new FileInputStream("/Users/chenyq/Documents/test.txt");
// 創(chuàng)建原始字節(jié)輸出流
OutputStream os = new FileOutputStream("/Users/chenyq/Documents/test1.txt");
// 將低級的字節(jié)輸入流包裝為高級的緩存輸入流
InputStream bis = new BufferedInputStream(is);
// 將低級的字節(jié)輸出流包裝為高級的緩存輸出流
OutputStream bos = new BufferedOutputStream(os);
) {
// 文件拷貝: 讀寫操作
byte[] arr = new byte[1024];
int len;
while ((len = bis.read(arr)) != -1) {
bos.write(arr, 0, len);
}
} catch(Exception e) {
e.printStackTrace();
}
}
字符緩存流
字符緩存輸入流
字符緩沖輸入流:實現(xiàn)類BufferedReader。
作用:提高字符輸入流讀取數(shù)據(jù)的性能,除此之外多了按照行讀取數(shù)據(jù)的功能。
字符緩存輸入流構(gòu)造器如下:
構(gòu)造器 | 說明 |
---|---|
BufferedReader(Reader r) | 可以把低級的字符輸入流包裝成一個高級的緩沖字符輸入流管道,從而提高字符輸入流讀數(shù)據(jù)的性能 |
public static void main(String[] args) throws Exception {
// 創(chuàng)建原始字符輸入流
Reader r = new FileReader("/Users/chenyq/Documents/test.txt");
// 將原始字符輸入流包裝為字符緩存輸入流
Reader br = new BufferedReader(r);
}
字符原始輸入流的方法字符緩存輸入流同樣適用:
public static void main(String[] args) {
try (
// 創(chuàng)建原始字符輸入流
Reader r = new FileReader("/Users/chenyq/Documents/test.txt");
// 將原始字符輸入流包裝為字符緩存輸入流
Reader br = new BufferedReader(r);
){
// 讀取文件
char[] arr = new char[1024];
int len;
while ((len = br.read(arr)) != -1) {
String res = new String(arr, 0, len);
System.out.println(res);
}
} catch (Exception e) {
e.printStackTrace();
}
}
字符緩存輸入流在原有的方法上新增了方法:
方法 | 說明 |
---|---|
readLine() | 讀取一行數(shù)據(jù), 并返回該行內(nèi)容的字符串,無行可讀返回null |
按行讀取: 普通方法
public static void main(String[] args) {
try (
Reader r = new FileReader("/Users/chenyq/Documents/test.txt");
BufferedReader br = new BufferedReader(r);
){
// 讀取第一行
System.out.println(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
按行讀取: 循環(huán)方法
public static void main(String[] args) {
try (
Reader r = new FileReader("/Users/chenyq/Documents/test.txt");
BufferedReader br = new BufferedReader(r);
){
String res;
while ((res = br.readLine()) != null) {
System.out.println(res);
}
} catch (Exception e) {
e.printStackTrace();
}
}
字符緩存輸出流:
字符緩沖輸出流:實現(xiàn)類BufferedWriter。
作用:提高字符輸出流寫取數(shù)據(jù)的性能,除此之外多了換行功能
字符緩存輸出流構(gòu)造器如下:
構(gòu)造器 | 說明 |
---|---|
BufferedWriter(Writer w) | 可以把低級的字符輸出流包裝成一個高級的緩沖字符輸出流管道,從而提高字符輸出流寫數(shù)據(jù)的性能 |
public static void main(String[] args) throws Exception {
Writer w = new FileWriter("/Users/chenyq/Documents/test.txt");
// 將原始字符輸出流包裝為緩沖輸出流
Writer bw = new BufferedWriter(w);
}
字符緩存輸出流新增功能:文章來源:http://www.zghlxwxcb.cn/news/detail-791122.html
當(dāng)然原生輸輸出流的功能同樣可以使用文章來源地址http://www.zghlxwxcb.cn/news/detail-791122.html
方法 | 說明 |
---|---|
newLine() | 換行操作 |
public static void main(String[] args) {
try (
Writer w = new FileWriter("/Users/chenyq/Documents/test.txt", true);
BufferedWriter bw = new BufferedWriter(w);
){
// 定義一個字符數(shù)組
char[] arr = {'我', '愛', 'C', 'h', 'i', 'n', 'a'};
// 定義一個字符串
String str = "我愛學(xué)習(xí)Java";
// 寫入一個字符
bw.write('我');
bw.write(98);
// 換行
bw.newLine();
// 寫入一個字符數(shù)組
bw.write(arr);
// 換行
bw.newLine();
// 寫入字符數(shù)組的一部分
bw.write(arr, 1, 3);
// 換行
bw.newLine();
// 寫入一個字符串
bw.write(str);
// 換行
bw.newLine();
// 寫入一個字符串的一部分
bw.write(str, 0, 4);
// 換行
bw.newLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
到了這里,關(guān)于Java IO流 - 緩沖流的詳細使用介紹的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!