Java基礎 筆記(六)

來源:互聯網
上載者:User

標籤:java   io   

IO:
按流向:
輸入資料流:程式可以從中讀取資料的流
輸出資料流:程式能向其中寫入資料的流
按傳輸單位:
位元組流:以位元組為單位傳輸資料的流
字元流:以字元為單位傳輸資料的流
按功能:
節點流:用於直接操作目標裝置的流
處理流:是對一個已經存在的流的串連和封裝,通過對資料提供處理為程式提供更為強大、靈活的讀寫功能
IO(Input Output)輸入輸出
讀寫檔案操作:
1 建立File類對象,定位檔案。
2 建立流
3 讀、寫
4 關閉流

字元流:
Writer:
FileWriter(String fileName)
FileWriter(String fileName,boolean append)

    void write(int c):一個字元一個字元的寫入    void write(char[] c)    void write(char[] c,int off,int len)    close()    flush()

Reader:
FileReader(String fileName) throws FileNotFoundException
int read() :傳回值 讀取的內容
int read(char[] c) :傳回值 讀取的長度
int read(char[] c,int off,int len) 傳回值 讀取的長度
read():當讀到檔案末尾,傳回值-1
close()
緩衝流:處理流
專門為了提高讀寫效率,必須結合讀取流來完成讀寫操作
先將資料讀取到緩衝區,然後從緩衝區讀取資料,效率高。
適用於檔案較大的情況
BufferedReader 字元緩衝區讀取流
BufferedWriter 字元緩衝區寫入流

—>裝飾者模式:基於已經存在的功能提供增強功能
將擴充的類作為新的類的建構函式參數傳入,然後對這個類進行裝飾處理。
為了讓裝飾者有被裝飾者的功能,需要存在繼承關係

字元流:FileWriter
向文字檔中寫入資料
1)使用Writer流
2)使用檔案字元流 FileWriter
構造方法:throws IOException
FileWriter(File file)
FileWriter(File file,boolean append)
FileWriter(String filename)
FileWriter(String fileName,boolean append)
方法:
void write(int c) :寫入單個字元
void write(char[] c) :寫入字元數組
void write(char[] c,intoff,int len): 寫入字元數組的一部分
void write(String str) :寫入字串
void write(String str,int off,intlen) :寫入字串的一部分
void flush() :重新整理該流的緩衝
void close() :關閉流,關之前會自動重新整理

// 建立檔案輸出資料流對象FileWriter
// 如果檔案不存在,會自動建立,如果存在會被覆蓋
FileWriter fw = new FileWriter(“src/temp.txt”);
// 向檔案中寫入資料,寫入到緩衝區中,這個緩衝區是一個位元組數組
fw.write(“hello world!!”);
// 重新整理緩衝區
fw.flush();
fw.write(“aa”);
fw.close(); // 自動重新整理緩衝區後,關閉流.關閉流之後就不可以寫入了,否則會拋出異常
System.out.println(“寫入成功”);
System.out.println(“—————”);
// 寫入的新的內容會在原來內容後面追加
FileWriter fw1 = new FileWriter(“src/temp.txt”, true);
fw1.write(“你好!!”);
fw1.flush();
fw1.close();

Reader類:FileReader
構造方法: throws FileNotFoundException
FileReader(File file)
FileReader(String fileName)
方法:throws IOException
int read():每次讀取一個字元,末尾-1
傳回值就是讀入的內容
int read(char[] c):每次讀取一組字元,最多讀數組長度個,末尾-1
傳回值實際讀取的個數
int read(char[] c,int off,int len):每次讀取一組字元,最多len個, 資料存入數組從off開始,末尾-1
傳回值實際讀取的個數
void close()

int num = 0;
while ((num = fr.read()) != -1) {// 每次讀取一個字元,末尾傳回值為-1
System.out.println((char) num);
}
fr.close();

BufferedWriter
處理流(不直接操作檔案)
目的:快取資料,提高寫入的速度。類似記憶體
BufferedWriter(Writer writer)
newLine()—>void 寫入一個新行
// 因為緩衝區流不具備寫入的功能,所以先建立檔案寫入流
FileWriter fw = new FileWriter(“src/temp.txt”);
// 為了提高效率,使用緩衝區流。把需要被提高寫入效率的流作為參數傳遞給緩衝區寫入流的構造方法
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 1; i <= 10; i++) {
bw.write(“你好” + i);
bw.newLine(); // 跨平台
bw.flush();
}
bw.close(); // 關閉緩衝區流就是關閉寫入流

BufferedReader:BufferedReader(Reader reader)
String readLine():每次讀取一行資料,但是返回的內容不包括換行,如果末尾,null
// 使用緩衝區讀取流檔案讀取
// 建立檔案讀取流和檔案相關聯的緩衝區讀取流
FileReader fr = new FileReader(“src/temp.txt”);
// 為了提高效率,使用緩衝區讀取流
BufferedReader br = new BufferedReader(fr);
// 提供了一個一次讀取一行的功能String readLine()
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println(br.readLine());// null
br.close(); // 自動關閉FileReader對象

位元組流:
InputStream類
FileInputStream
OutputStream類
FileOutputStream
// 返迴文件的大小,單位位元組 int num = fis.available(); System.out.println(“檔案大小:”+ num + “bytes”);
byte[] arr = new byte[num]; // 定義了一個和檔案大小相同的位元組數組 int len =fis.read(arr);// 不適用檔案過大的情況 System.out.println(new String(arr, 0,len));
// 一個位元組一個位元組讀取:中文會出現亂碼,每次讀取的是一半漢字
int num = 0;
while ((num =fis.read()) != -1) {
System.out.println((char) num);
}

// 讀取後放入數組中 byte[] b = new byte[1000]; int len = 0;
while ((len =fis.read(b)) != -1) {
System.out.println(new String(b, 0, len));
}
fis.close();

// 標準輸入資料流
InputStream is = System.in;
// 標準輸入位元組流轉化成字元流
InputStreamReader isr = new InputStreamReader(is);
// 使用緩衝區流提高效率
BufferedReader br = new BufferedReader(isr);
// 標準輸出資料流:列印流
PrintStream out = System.out;
// 建立輸出資料流對象
OutputStreamWriter osw = new OutputStreamWriter(out);
// 建立緩衝區輸出資料流
BufferedWriter bw = new BufferedWriter(osw);
String line = null;
while ((line = br.readLine()) != null) {
if (line.equals(“88”)) {
bw.write(“謝謝使用”);
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();

File:

基本方法:
File f05 = new File(“src/Demo1.java”);
System.out.println(“檔案名稱為:” + f05.getName());
System.out.println(“檔案的路徑為:” + f05.getPath());
System.out.println(“父路徑為:” + f05.getParent());
System.out.println(“絕對路徑:” + f05.getAbsolutePath());
System.out.println(“檔案的長度為:” + f05.length() + “B”);

    System.out.println("Demo1.java是否是一個檔案?" + f05.isFile());    System.out.println("Demo1.java是否是一個目錄?" + f05.isDirectory());    System.out.println(new File("e:/abc").delete());    System.out.println(new File("e:/abc/123/456/789").delete());

FileFilter:檔案過濾器
// 顯示所有的txt
static void listTxt(File file) {
File[] files = file.listFiles(new FileFilter() {

        @Override        public boolean accept(File pathname) {            String name = pathname.getName();            if (name.endsWith("txt")) {                return true;            }            return false;        }    });    for (File f : files) {        System.out.println(f.getName());    }}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java基礎 筆記(六)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.