LoginSignup
10
8

More than 5 years have passed since last update.

テキストファイル(二次元配列)読み込み

Posted at
public static int[][] Fileimport(String adress){

        /* adress = 読み込みたいテキストファイルがある場所。
         */

        //配列[ノード番号][ノードの要素]の配列を準備
        int Index[][] = new int[325557][];

        //ファイルの読み込み
        try{
            //ファイルのアドレスよりbrにファイルの情報を格納
            FileReader filereader = new FileReader(adress);
            BufferedReader br = new BufferedReader(filereader);

            //一行ずつ読み込んで
            String str = br.readLine();
            int count = 0;

            while(str != null){
                //strlist.add(str.split(","));
                //一行の内容を','で分割してそれぞれを[count=ノード番号]の2次元目の配列の要素として格納
                Index[count] = parseInts(str.split(","));
                //次の行を読み込み
                str = br.readLine();
                count++;
            }

            br.close();

        }catch(FileNotFoundException e){
            System.out.println(e);
        }catch(IOException e){
            System.out.println(e);
        }

        return Index;

    }
public static int[] parseInts(String[] s){

        /* s[] = intに変換したいストリングを収めた配列
         */

        int[] x = new int[s.length];
        for(int i = 0; i < s.length; i++){
            x[i] = Integer.parseInt(s[i]);
        }
        return x;
    }
10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8