简体   繁体   English

逐字符读取文本文件到2d char [] []数组中

[英]Reading text file character by character into a 2d char[][] array

I have to read in a text file called test.txt. 我必须阅读一个名为test.txt的文本文件。 the first line of the text file are two integers. 文本文件的第一行是两个整数。 these integers tell you the rows and columns of the 2D char array. 这些整数告诉您2D char数组的行和列。 The rest of the file contains characters. 文件的其余部分包含字符。 the file looks a bit like: 4 4 FILE WITH SOME INFO except vertically on top of one another not horizontally. 该文件看起来有点像:4 4具有某些信息的文件,除了垂直彼此叠置而不是水平叠置。 I must then read each of the rest of the contents of the file into the 2D char[][] array using nested for loops. 然后,我必须使用嵌套的for循环将文件的其余所有内容读取到2D char [] []数组中。 I am not supposed to copy from one array to another. 我不应该从一个数组复制到另一个数组。 This is the code I have so far. 这是我到目前为止的代码。 I'm having trouble reading each character line by line into my 2D char array. 我无法逐行将每个字符读取到2D char数组中。 Help been working at this for hours. 帮助工作了几个小时。

    public static void main(String[] args)throws IOException 
{



    File inFile = new File("test.txt");                                 
    Scanner scanner = new Scanner(inFile);     
    String[] size = scanner.nextLine().split("\\s");         

    char[][] array = new char[Integer.parseInt(size[0])][Integer.parseInt(size[1])];

    for(int i=0; i < 4; i++) {
        array[i] = scanner.nextLine().toCharArray();
    }

    for(int k = 0; k < array.length; k++){
        for(int s = 0; s < array[k].length; s++){
            System.out.print(array[k][s] + " ");
        }
        System.out.println();
    }

     scanner.close();


}

} }

If I have understood it correctly - file format is something like 如果我正确理解-文件格式类似

4 4
FILE
WITH
SOME
INFO

Modify as below 修改如下

    Scanner scanner = new Scanner(inFile);     
    String[] size = scanner.nextLine().split("\\s");         

    char[][] array = new char[Integer.parseInt(size[0])][Integer.parseInt(size[1])];

    for(int i=0; i < rows; i++) {
        array[i] = scanner.nextLine().toCharArray();
    }

Above code is for initialization of you char array. 上面的代码用于初始化char数组。 In order to print the same you can do something like 为了打印相同的内容,您可以执行以下操作

Arrays.deepToString(array);

Copying Tirath´s file format: 复制Tirath的文件格式:

4 4 FILE WITH SOME INFO 4 4具有某些信息的文件

I would pass it into a 2d array as follows: 我将其传递到二维数组中,如下所示:

public static void main(String[] args){

    char[][] receptor = null;   //receptor 2d array
    char[] lineArray = null;    //receptor array for a line

    FileReader fr = null;
    BufferedReader br = null;
    String line = " ";

    try{
    fr = new FileReader("test.txt");
    br = new BufferedReader(fr);

        line = br.readLine();//initializes line reading the first line with the index
        int i = (int) (line.toCharArray()[0]-48); //we convert line to a char array and get the fist index (i) //48 = '0' at ASCII
        int j = (int)(line.toCharArray()[1]-48); // ... get the second index(j)

        receptor = new char[i][j];  //we can create our 2d receptor array using both index

        for(i=0; i<receptor.length;i++){
                line = br.readLine(); //1 line = 1 row
                lineArray = line.toCharArray(); //pass line (String) to char array
            for(j=0; j<receptor[0].length; j++){ //notice that we loop using the length of i=0
                receptor[i][j]=lineArray[j];    //we initialize our 2d array after reading each line
            }
        }

    }catch(IOException e){
        System.out.println("I/O error");
    }finally{
        try {
            if(fr !=null){
            br.close();
            fr.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } //end try-catch-finally
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM