简体   繁体   中英

File Input/Output, Storing Data

Alright, lets try this again, sorry for the poor title I don't know what to call this. I haven't done much File I/O before, I personally think Java is absolutely terrible at it but nonetheless I have to do it.

A bit of background information, this is my Conway's Game of Life project, and all of my other code works fine I just need to do the I/O now.

I'm reading in from a text file, that looks like this .

So what this does is the first two numbers are the size of that the array needs to be, the String after them is irrelevant. And after that is the array of the cell arrangement in this example it's that of the glider. Now my problem is that I don't know how to store the first line in a String, and the rest in an array. Below is what I've done, and brace yourselves for a Big O of n^∞.

    public char[][] fileIO(){
    ArrayList<String> list = new ArrayList<String>();
    char[][] newArray;
    String[] tokens;
    BufferedReader reader;
    String inputLine;

    try{
        reader = new BufferedReader(new FileReader("untitled.txt"));
        while((inputLine = reader.readLine()) != null){
            list.add(inputLine);
        }
        reader.close();
    }catch (IOException e){
        System.out.println(e);
    }

    tokens = list.get(0).trim().split("\\s+");
    newArray = new char[Integer.parseInt(tokens[0])][Integer.parseInt(tokens[1])];

    for(int row = 0; row < newArray.length; row++){
        for(int col = 0; col < newArray[row].length; col++){
            for(int line = 1; line < list.size(); line++){
                for(int Char = 0; Char < list.get(line).length(); Char++){
                    newArray[row][col] = list.get(line).charAt(Char);
                }
            }
        }
    }
    return newArray;
}

I know there's a lack of comments, so I'll try to explain what I tried to do now;

  • I read from the file and stored it all in an ArrayList

  • Got the tokens of the first like (10 20 Glider), and created an instantiated an array with the values 10 and 20

  • Then proceeded to loop through the array's rows and columns, then each line of the inputted array, and then looped through each character of that line to add into the array at their respected positions.

However, this doesn't seem to be working and I'm sure there is a much simpler and efficient way of doing this. So I ask again, can anyone tell me of a simpler way of doing this I/O? I don't need the code, just an explanation of how to do so.

In this:

for(int row = 0; row < newArray.length; row++){
        for(int col = 0; col < newArray[row].length; col++){
            for(int line = 1; line < list.size(); line++){
                for(int Char = 0; Char < list.get(line).length(); Char++){
                    newArray[row][col] = list.get(line).charAt(Char);
                }
            }
        }
    }

You have two outer loops that are browsing through your newArray grid, and two inner loops that are browsing through the lines of the file. By doing that, you are essentially writing every char of every lines of your file into each cell of your grid, one after the other. In the end, you will have the last character of the last line of your file in all the cells of newArray . This is probably not what you want to do.

Edit: If I understand correctly, the number at the beginning of the file are the number of lines and the number of columns of the data that follow. Then you just have to use the two outer loops, and use row and col to access respectively the line in list and the character in the line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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