将txt文件读入2d数组java

时间:2016-04-30 16:01:01

标签: java arrays 2d

非常初学java。我的程序只打印一个长列而不是二维数组。为了测试,我使用一个包含10行和20列的文件,但实际文件将是20列和几千行,我不知道有多少。我已经阅读了我在互联网上可以找到关于这个主题的所有帖子,但仍然无法让程序运行。有什么想法吗?

 1 2 4 6 9 13 15 16 21 28 34 37 41 48 50 52 53 54 57 68
 6 7 10 17 23 24 27 28 31 39 42 43 46 48 50 55 60 61 67 70
 2 3 5 7 11 14 15 20 28 45 46 47 48 52 56 61 62 63 66 70
 4 5 7 11 13 15 19 23 24 27 28 35 38 40 48 50 57 58 64 66
 3 8 20 26 27 32 36 38 39 43 45 47 50 53 54 56 59 61 67 68
 1 3 5 7 15 19 26 30 31 36 41 44 48 49 56 58 59 60 61 65
 1 2 4 6 9 13 15 16 21 28 34 37 41 48 50 52 53 54 57 68
 6 7 10 17 23 24 27 28 31 39 42 43 46 48 50 55 60 61 67 70
 2 3 5 7 11 14 15 20 28 45 46 47 48 52 56 61 62 63 66 70
 4 5 7 11 13 15 19 23 24 27 28 35 38 40 48 50 57 58 64 66

这是代码

try {
    FileInputStream fstream = new FileInputStream("C:\\keno.txt");

    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine = "";
    String[] tokens;

    //Read File Line By Line
    int row = 0;
    String userdata [][]= new String [10][21];
    while ((strLine = br.readLine()) != null)   {
        // Copy the content into the array
        tokens = strLine.split(" +");
        for(int j = 0; j < tokens.length; j++) {
            userdata[row][j] = tokens[j];
        }
        row++;
    }
    for(int i = 0; i < userdata.length; i++) {
        for(int j=0; j < userdata[i].length; j++){
            System.out.println(userdata[i][j]);
        }  
    }   
    in.close();
} catch (Exception e) {
    System.err.println("Error: " + e.getMessage());
}

1 个答案:

答案 0 :(得分:0)


我写了一个简短的程序,做我认为你想做的事情。我的代码首先将txt文件中的所有数据提取到stringarray,然后用分隔符“”(空格)分割每个字符串。最后,字符串被解析为长格式。希望这能帮助你。
重要说明:txt文件必须具有相同的尺寸,这意味着如果第一行有20个元素,则每行必须有20个元素。 AND:每行必须以元素结尾,而不是以空格结尾。让我知道这是否有用,如果没有,你遇到麻烦!
格尔茨

import java.io.*;
import java.util.ArrayList;

public class ReadFileInto2dArray{

    public static void main(String[] arg) throws FileNotFoundException, IOException, NumberFormatException {

        // IMPORT DATA FROM TXT FILE AS STRINGARRAY
        String filedirectory = "C:\\Users\\thomas\\Desktop\\Neuer Ordner\\keno.txt";
        BufferedReader b = new BufferedReader(new FileReader(filedirectory));
        String str=null;
        ArrayList<String> lines = new ArrayList<String>();
        while((str = b.readLine()) != null){
            lines.add(str);
        }
        String[] strArr = lines.toArray(new String[lines.size()]);  
        b.close();

        // GET DIMENSIONS: number of rows       
        int nRows = strArr.length;                                                                          
        // GET DIMENSIONS: number of elements in the first line
        int nCols = (strArr[0].length()-strArr[0].replace(" ", "").length())+1;     

        // INITIALIZE LONG 2D ARRAY (MATRIX)
        long[][] data = new long[nRows][nCols];

        // SPLIT EACH STRING OF ROW INTO SUBSTRING AND PARSE TO LONG FORMAT
        String[] split = new String[nCols];
        for (int r=0; r<nRows; r++){
            split = strArr[r].split(" ");
            for (int c=0; c<nCols; c++) {
                data[r][c] = Long.parseLong(split[c]);
            }
        }
        b.close();

        // SHOW THAT IT WORKED
        System.out.println("first element: data[0][0] should be 1: " + data[0][0]);
        System.out.println("last element data[9][19] should be 66: " + data[9][19]);

    }
}
相关问题