首頁>Club>
21
回覆列表
  • 1 # 悠然設計

    在之前的學習過程中,經常會遇到將文字檔案中的資料讀取到陣列或其他資料結構中。每次遇到,總是在網上搜索程式碼解決,解決之後並沒有總結複習,因此在下一次遇到同樣的問題時,又重複之前的過程。這樣週而復始,並沒有將知識積累下來,其實是把自己給坑了(對此深有體會)。因此經過兩天的學習,對檔案讀取這一方面有了一定的瞭解,寫下部落格,總結一些東西,以備後續查詢。

    文字檔案讀取的大致過程如下:

    構建檔案物件,

    使用檔案物件構造Reader物件可以是FileReader、InputStreamReader等

    使用Reader對像構建BufferedReader物件(主要使用其readLine()方法,用於按行讀取檔案)

    按行讀取檔案,將每行獲取到的字串進行處理。

    下面給出使用FileReader實現將文字檔案讀取至一維陣列:

    public static int[] toArrayByFileReader1(String name) {

    // 使用ArrayList來儲存每行讀取到的字串

    ArrayList<String> arrayList = new ArrayList<>();

    try {

    FileReader fr = new FileReader(name);

    BufferedReader bf = new BufferedReader(fr);

    String str;

    // 按行讀取字串

    while ((str = bf.readLine()) != null) {

    arrayList.add(str);

    }

    bf.close();

    fr.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    // 對ArrayList中儲存的字串進行處理

    int length = arrayList.size();

    int[] array = new int[length];

    for (int i = 0; i < length; i++) {

    String s = arrayList.get(i);

    array[i] = Integer.parseInt(s);

    }

    // 返回陣列

    return array;

    }

    用InputStreamReader方式:

    public static int[] toArrayByInputStreamReader1(String name) {

    // 使用ArrayList來儲存每行讀取到的字串

    ArrayList<String> arrayList = new ArrayList<>();

    try {

    File file = new File(name);

    InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file));

    BufferedReader bf = new BufferedReader(inputReader);

    // 按行讀取字串

    String str;

    while ((str = bf.readLine()) != null) {

    arrayList.add(str);

    }

    bf.close();

    inputReader.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    // 對ArrayList中儲存的字串進行處理

    int length = arrayList.size();

    int[] array = new int[length];

    for (int i = 0; i < length; i++) {

    String s = arrayList.get(i);

    array[i] = Integer.parseInt(s);

    }

    // 返回陣列

    return array;

    }

    使用RandomAccessFile方式:

    public static int[] toArrayByRandomAccessFile(String name) {

    // 使用ArrayList來儲存每行讀取到的字串

    ArrayList<String> arrayList = new ArrayList<>();

    try {

    File file = new File(name);

    RandomAccessFile fileR = new RandomAccessFile(file,"r");

    // 按行讀取字串

    String str = null;

    while ((str = fileR.readLine())!= null) {

    arrayList.add(str);

    }

    fileR.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    // 對ArrayList中儲存的字串進行處理

    int length = arrayList.size();

    int[] array = new int[length];

    for (int i = 0; i < length; i++) {

    String s = arrayList.get(i);

    array[i] = Integer.parseInt(s);

    }

    // 返回陣列

    return array;

    }

    以上給出了三種方式實現將文字檔案讀取至陣列(當然檔案肯定是有規律的),下面給出將檔案讀取至二維陣列的方法(其實與上面的方法一樣,只不過在後面處理ArrayList時採用的方式不一樣。)

    將文字檔案讀取至二維陣列(以InputStreamReader為例):

    public static int[][] toArrayByInputStreamReader2(String name) {

    // 使用ArrayList來儲存每行讀取到的字串

    ArrayList<String> arrayList = new ArrayList<>();

    try {

    File file = new File(name);

    InputStreamReader input = new InputStreamReader(new FileInputStream(file));

    BufferedReader bf = new BufferedReader(input);

    // 按行讀取字串

    String str;

    while ((str = bf.readLine()) != null) {

    arrayList.add(str);

    }

    bf.close();

    input.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    // 對ArrayList中儲存的字串進行處理

    int length = arrayList.size();

    int width = arrayList.get(0).split(",").length;

    int array[][] = new int[length][width];

    for (int i = 0; i < length; i++) {

    for (int j = 0; j < width; j++) {

    String s = arrayList.get(i).split(",")[j];

    array[i][j] = Integer.parseInt(s);

    }

    }

    // 返回陣列

    return array;

    }

    最後附上文字檔案的格式,如下圖所示

    ---------------------

    原文:https://blog.csdn.net/milletguo/article/details/80144290

  • 中秋節和大豐收的關聯?
  • 《奧特曼》最強排名前20有誰?