java读取txt文件写入数组

txt文件是一个30000*4的数组阵列

参考代码如下:

public class FileReaderForRead
{
    public static void main(String [ ] args)
    {
        
        try {
            System.out.println(System.in);

            FileReader fileReader = new FileReader("D:\\import_users.txt");
            BufferedReader buf = new BufferedReader(fileReader);

            int i = 0;
            String bufToString = "";
            String readLine = "";
            String[] myArray = new String[500];  //100:这个值你自己定义,但不宜过大,要根据你文件的大小了,或者文件的行数
            while((readLine = buf.readLine()) != null){
                myArray[i] = readLine;
                i++;
            }
       }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-01

数组的动态性不好。建议先用List<String[]>,若需要,再转化为二维数组

public class $ {
    public static void main(String[] args) {

        List<String[]> data = new ArrayList<>();

        try {
            Scanner in = new Scanner(new File("D:/a.txt"));

            while (in.hasNextLine()) {
                String str = in.nextLine();

                data.add(str.split(" ,|"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // list转化为数组
        String[][] result = data.toArray(new String[][] {});

        for (String[] strings : result) {
            for (String string : strings) {
                System.out.print(string + " ");
            }
            System.out.println();
        }
    }
}

追问

有企鹅么 ?希望能够 得到 帮助

追答

1 2 1 6 6 5 6 8 9

本回答被提问者和网友采纳
相似回答