歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java 緩沖流 Buffer

Java 緩沖流 Buffer

日期:2017/3/1 9:08:29   编辑:Linux編程

緩沖流 Buffer :設置緩沖區加快執行效率

子類:

 (一)BufferedInputStream : 緩沖輸入字節流 ,目的:提高讀取文件的效率
   注意: BufferedInputStream 他是沒有讀寫數據的功能
  內部實現 : 你面維護了一個8字節的byte數組。
  使用步驟:
1.找到一個目標文件.
2.建立通道 new FileInputStream(" ");
3.創建一個緩沖字節輸入流 new BufferedInputStream(FileInputStream);
4.讀取數據 read();
5.關閉資源 close();

 (二)BufferedOutputStream :緩沖輸出字節流 內部維護了一個 8k的字節數組
  作用: 提高文件的輸出的效率,可以提供其他的方法。
  使用:
   1.找目標
   2.建立通道 FileOutputStream
   3.創建一個緩沖字節輸出流
   4.寫數據,不會寫入到磁盤中。 如果數組中的數據已經滿了,才會自動將數據寫入到磁盤中。
  5.將數據寫入到磁盤 : 調用flush(),或者關閉資源。
   6.關閉資源。

 (三)BuffredRead 緩沖輸入字符流。
   有一個擴展的功能:readLine(); // 可以一次讀一行文字。

 (四)BufferedWriter: 緩沖輸出字符流
內部提供一個8192長度的字符數組作為這樣一個緩沖區。
   BufferedWriter作用 :提高寫入的效率,拓展FileWriter的功能。

    newLine(); //換行寫入數據

簡單的字節緩沖流案例

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class buffered {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//bufInTest();
bufOutTest();
}

//(1)BufferedInputStream 的使用
public static void bufInTest() throws IOException{
//1.找到目標
File file = new File("D:\\a.txt");
//2.創建通道
FileInputStream fis = new FileInputStream(file);

//**3.創建一個緩沖輸入字節流******
BufferedInputStream bfis = new BufferedInputStream(fis);

//4.開始寫入數據
int content = 0; // 一次只會取一個字節
while ((content= bfis.read())!= -1) { // 還是一個一個的讀取 問什麼可以提高效率呢?
System.out.print((char)content);
}
//5.關閉通道
bfis.close();
}

//(2)BufferedOutputStream 的使用
public static void bufOutTest() throws IOException{

//1.找目標
File file = new File("D:\\b.txt");
//2.創建通道
FileOutputStream fos = new FileOutputStream(file);

//3.創建緩沖流
BufferedOutputStream bfos = new BufferedOutputStream(fos);

//4.創建寫入文件的數據
String string = "good good study day day up";

//5.寫數據, 到這一步只是將數據保存到內存中字節數組中。
bfos.write(string.getBytes());

//6.再刷新 將數據寫入到磁盤中
//bfos.flush();

//7.關閉資源
bfos.close();//內部會實現flush();
}
}

簡單的字符緩沖流案例

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class fileReader {

public static void main(String[] args) throws IOException {
testBufferedWriter();
testBufferedRead();
}
//(1)緩沖輸出字符流的使用
public static void testBufferedWriter() throws IOException{

//1.建立一個通道,指定一個路徑
FileWriter fiw = new FileWriter("D:\\a.txt",true);

//2.創建緩沖流
BufferedWriter bfw = new BufferedWriter(fiw);

//讓數據換行顯示
bfw.newLine(); //換行寫入數據

//3.開始寫入數據
bfw.write("上課不要吃東西");

//4.關閉資源
bfw.close(); // 關閉資源之前會做一個刷新操作。調用flush()方法。
}

//(2)緩沖輸入字符流的使用
public static void testBufferedRead() throws IOException{

//1.創建通道並且指定路徑
FileReader fir = new FileReader("D:\\b.txt");

//2.創建緩沖流
BufferedReader bfr = new BufferedReader(fir);

//3.1、開始讀取數據(一次讀取一個字節)
int content = 0;
while ((content = bfr.read()) != -1) {

System.out.print((char)content);
}

//3.2、readLine()擴展功能,讀取一行數據
String string1 = bfr.readLine();
System.out.println(string1);

//一次讀取一行數據(返回字符串),效率更高
String string = null;
while ((string = bfr.readLine()) != null) {
System.out.println(string);
}

//4.關閉
bfr.close();
}
}

Copyright © Linux教程網 All Rights Reserved