JAVA流讀取文件並保存數據


如圖有文本如下數據

寫方法讀取數據

 private String[][] getData(){
        // 使用ArrayList來存儲每行讀取到的字符串
        ArrayList<String> arrayList = new ArrayList<>();
        try {
            File file = new File("D:/aaa.txt");
            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();
        String array[][] = new String[length][2];
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < 2; j++) {
                String s = arrayList.get(i).split(",")[j];
                array[i][j] = s;
            }
        }
        // 返回數組
        return array;
    }

實現數據的讀取


免責聲明!

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



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