java 按行读取txt文件并存入数组

 	/**
     * 从文件中载入测试集
     *
     * @throws IOException
     */
    public static ArrayList<Tx> loadTestCases(String fileName) throws IOException {

        System.out.println("================================ 1。 end load testSet ================================");

        ArrayList<Tx> testSet = new ArrayList<>(); // 测试集

        BufferedReader br = null;
        try {

            br = new BufferedReader(new FileReader(new File(fileName)));

            String line = null;

            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (!line.isEmpty()) {

                    String[] info = line.split(" ");

                    Tx tx = new Tx(Integer.parseInt(info[1]), Integer.parseInt(info[2]));
                    testSet.add(tx);

                }
            }

        }
        finally {
            if (br != null) {
                br.close();
            }
        }

        // 打印测试
        for(int i = 0;i < testSet.size(); i ++){
            System.out.println(testSet.get(i));
        }

        System.out.println("================================ 2。 end load testSet ================================");

        return testSet;
    }

供众号:微程序学堂

猜你喜欢

转载自blog.csdn.net/u013288190/article/details/124332801