Java IO Reader,Writer類 讀寫字符 用法詳解教程。

Reader 和 writer 是轉麼用來讀取字符的,避免了很多的字符亂碼問題,讀取字符十分的方便。但是不能像InputStream 和 OutputStream 用於讀取圖片,視頻等,只能用於讀取字符.

一、Reader類讀取字符,

1. 構造方法,

在這裏插入圖片描述
我們這裏使用 FileReader類

Reader reader = new FileReader(url);

url用法和之前的File,Input/Output Stream一樣,File的實例或者 是 路徑的字符串.

2. 常用方法。(這裏的用法和InputStream基本一致)

  1. read(char [])
  2. close()。

3. 使用步驟

  • 1.構造
  • 2.int read(char[] cbuf,
    int off,
    int len)
    在這裏插入圖片描述
  • 3.close()

實例代碼.

package Java學習.Java高級.IO.IO.Reader字符讀取;

import java.io.FileReader;
import java.io.IOException;

/**
* 一、構造:
*  Reader reader = new FileReader(url);
* 二、步驟
* 1.構造
* 2.read(char[],int index,int length)
* 或者 read(char[])
* 3.close()
* 三:read()和
* InputStream: read(byte[])
* Reader: read(char[])
* 會把字節或者字符 儲存到 數組裏,
*/
public class Mian {
   public static void main(String[] args) throws IOException {
       String url =  "D:\\Program Files\\JetBrains\\test1\\Lab" +
               "\\src\\Java學習\\Java高級\\IO\\IO\\Reader字符讀取\\";
       FileReader fileReader = new FileReader(url + "test.txt");
       char[] chars = new char[1024];
       while (fileReader.read(chars,0,chars.length) != -1){
           System.out.println(new String(chars));
       }
   }
}

Run:
132半畝方糖acb

Process finished with exit code 0

Writer

1.構造同上

Writer writer = new FileWriter(url,true);

url : 寫入的路徑
true : append 允許向後添加寫入。不覆蓋

2. 常用方法

  • 1.write(char[] chars, int off, int len) 寫道內存
  • 2.flush() 刷到硬盤
  • 3.close() 關閉流.

3. 使用步驟

  • 1.構造,使用多態
  • 2.write(char[] chars, int off, int len)
  • 3.flush()
  • 4.close()

代碼實例:

package Java學習.Java高級.IO.IO.Writer寫入;

import java.io.*;

/**
* 一、注意:Reader 是把數據寫入到內存中,要使用flush刷新內存中的數據到硬盤
* 二、構造方法和Reader一樣
* 三、常用方法
* 1.write(char[] chars, int off, int len) 寫道內存
* 2.flush() 刷到硬盤
* 3.close() 關閉流.
* 四、步驟
* 1.構造,使用多態
* 2.write(char[] chars, int off, int len)
* 3.flush()
* 4.close()
*/
public class Main {
   public static void main(String[] args) throws IOException {
       String url = "D:\\Program Files\\JetBrains\\tes" +
               "t1\\Lab\\src\\Java學習\\Java高級\\IO\\IO\\Writer寫入\\文件寫入測試.txt";
       Writer writer = new FileWriter(url,true);
       for (int i = 0; i < 10; i++) {
           String string = "這是第 "+(i+1)+"個字符串\n";
           char[] chars = string.toCharArray();
           writer.write(chars);//寫入內存,必須寫入硬盤才行。
           writer.flush();//寫入硬盤
           System.out.println("寫入的字符爲:"+new String(chars,0,chars.length));
       }
   	writer.close();//關閉流.
   }
}

4. write() 可以直接寫字符串 String 代替 char[];所以上面的代碼還可以寫爲(更簡潔)

package Java學習.Java高級.IO.IO.Writer寫入;

import java.io.*;

/**
 * 一、注意:Reader 是把數據寫入到內存中,要使用flush刷新內存中的數據到硬盤
 * 二、構造方法和Reader一樣
 * 三、常用方法
 * 1.write(char[] chars, int off, int len) 寫道內存
 * 2.flush() 刷到硬盤
 * 3.close() 關閉流.
 * 四、步驟
 * 1.構造,使用多態
 * 2.write(char[] chars, int off, int len)
 * 3.flush()
 * 4.close()
 */
public class Main {
    public static void main(String[] args) throws IOException {
        String url = "D:\\Program Files\\JetBrains\\tes" +
                "t1\\Lab\\src\\Java學習\\Java高級\\IO\\IO\\Writer寫入\\文件寫入測試.txt";
        Writer writer = new FileWriter(url,true);
        for (int i = 0; i < 10; i++) {
            String string = "這是第 "+(i+1)+"個字符串\n";
            writer.write(string);//寫入內存,必須寫入硬盤才行。
            writer.flush();//寫入硬盤
            System.out.println("寫入的字符爲:"+string);
        }
        writer.close();
    }
}

Run:
寫入的字符爲:這是第 1個字符串

寫入的字符爲:這是第 2個字符串

寫入的字符爲:這是第 3個字符串

寫入的字符爲:這是第 4個字符串

寫入的字符爲:這是第 5個字符串

寫入的字符爲:這是第 6個字符串

寫入的字符爲:這是第 7個字符串

寫入的字符爲:這是第 8個字符串

寫入的字符爲:這是第 9個字符串

寫入的字符爲:這是第 10個字符串

Process finished with exit code 0

文件寫入情況:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章