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;
    }

实现数据的读取

原文地址:https://www.cnblogs.com/feiyangbahu/p/10789117.html