百度上提问:Java上怎样读取txt文件里的数据为一个多行两列的数组,每一个数据由逗号隔开

2024年11月23日 00:06
有1个网友回答
网友(1):

public voidinit(String filename) throws FileNotFoundException, IOException{

        //读取数据

        double[] x;

        double[] y;

        int num;

        String strbuff;

        BufferedReader tspdata = newBufferedReader(new InputStreamReader(new FileInputStream(filename)));

        strbuff = tspdata.readLine();

        int num = Integer.valueOf(strbuff);

       // System.out.println(Integer.valueOf(strbuff));

        x = new double[num];

        y = new double[num];

        for (int citys = 0; citys

            strbuff = tspdata.readLine();

            String[] strcol =strbuff.split(",");

            x[citys] = Integer.valueOf(strcol[1]);

            y[citys] =Integer.valueOf(strcol[2]);

        }

    }