1. 程式人生 > 其它 >|NO.Z.00081|——————————|BigDataEnd|——|Java&IO流.V08|--------------------------------------------------|Java.v08|IO流.v08|緩衝字元流|使用|

|NO.Z.00081|——————————|BigDataEnd|——|Java&IO流.V08|--------------------------------------------------|Java.v08|IO流.v08|緩衝字元流|使用|



[BigDataJava:Java&IO流.V08]                                                                                    [BigDataJava.核心類庫] [|章節二|IO流|緩衝字元流的使用|]








一、BufferedWriter類(重點)
### --- 基本概念

——>        java.io.BufferedWriter類主要用於寫入單個字元、字元陣列以及字串到輸出流中。
二、常用的方法
方法宣告 功能介紹
BufferedWriter(Writer out)
根據引數指定的引用來構造物件
BufferedWriter(Writer out, int sz) 根據引數指定的引用和緩衝區大小來構造物件
void write(int c) 寫入單個字元到輸出流中
void write(char[] cbuf, int off, intlen) 將字元陣列cbuf中從下標off開始的len個字元寫入輸出流中
void write(char[] cbuf) 將字串陣列cbuf中所有內容寫入輸出流中
void write(String s, int off, int len) 將引數s中下標從off開始的len個字元寫入輸出流中
void write(String str)
將引數指定的字串內容寫入輸出流中
void newLine() 用於寫入行分隔符到輸出流中
void flush() 重新整理流
void close() 關閉流物件並釋放有關的資源
三、BufferedReader類(重點)
### --- 基本概念

——>        java.io.BufferedReader類用於從輸入流中讀取單個字元、字元陣列以及字串。
四、常用的方法
方法宣告 功能介紹
BufferedReader(Readerin) 根據引數指定的引用來構造物件
BufferedReader(Readerin, int sz) 根據引數指定的引用和緩衝區大小來構造物件
int read() 從輸入流讀取單個字元,讀取到末尾則返回-1,
否則返回實際讀取到的字元內容
int read(char[] cbuf, intoff, int len) 從輸入流中讀取len個字元放入陣列cbuf中下標從off開始的位置上,若讀取到末尾則返回-1,否則返回實際讀取到的字元個數
int read(char[] cbuf)  從輸入流中讀滿整個陣列cbuf
String readLine() 讀取一行字串並返回,返回null表示讀取到末尾
void close() 關閉流物件並釋放有關的資源
五、程式設計程式碼
package com.yanqi.task17;

import java.io.*;

public class BufferedCharCopyTest {

    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            // 1.建立BufferedReader型別的物件與d:/a.txt檔案關聯
            br = new BufferedReader(new FileReader("d:/a.txt"));
            // 2.建立BufferedWriter型別的物件與d:/b.txt檔案關聯
            bw = new BufferedWriter(new FileWriter("d:/b.txt"));
            // 3.不斷地從輸入流中讀取一行字串並寫入到輸出流中
            System.out.println("正在玩命地拷貝...");
            String str = null;
            while ((str = br.readLine()) != null) {
                bw.write(str);
                bw.newLine(); // 當前系統中的行分隔符是:\r\n
            }
            System.out.println("拷貝檔案成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4.關閉流物件並釋放有關的資源
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
六、編譯列印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=58500:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task17.BufferedCharCopyTest
正在玩命地拷貝...
拷貝檔案成功!

Process finished with exit code 0








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                    ——W.S.Landor



來自為知筆記(Wiz)