Java中文本文件的讀取(按行讀取)


在之前的學習過程中,經常會遇到將文本文件中的數據讀取到數組或其他數據結構中。每次遇到,總是在網上搜索代碼解決,解決之后並沒有總結復習,因此在下一次遇到同樣的問題時,又重復之前的過程。這樣周而復始,並沒有將知識積累下來,其實是把自己給坑了(對此深有體會)。因此經過兩天的學習,對文件讀取這一方面有了一定的了解,寫下博客,總結一些東西,以備后續查詢。

文本文件讀取的大致過程如下:

    構建文件對象,
    使用文件對象構造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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM