Write a file with the contents of an array

Asked

Viewed 421 times

0

I have an array of char grid and I want to write a file with its contents. Here’s the code I made:

public static String getGrid() {
    String text = String.valueOf(grid);
    return text;
}


public static void Escreve() {

    String imprime = getGrid();
    System.out.println(imprime);

    File newFile = new File("C:/Users/Miguel/Desktop/newFile.txt");
    if (newFile.exists()) {
        System.out.println("já existe");
    } else {
        try {
            newFile.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            FileWriter fileW = new FileWriter(newFile);
            BufferedWriter buffW = new BufferedWriter(fileW);
            buffW.write(imprime);
            buffW.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

The array grid originally would look as follows:

grid = {{'W','S','W', 'W'}, {'W','W','_','E'}}

the desired end result of the created file is:

WSWW
WW_E

the first "function" transforms the char array to string array and the second creates a new file and should write the contents of the array, but what I get in the created file is the following: [[C@2424d3cc

Some way to solve the problem?

  • You can add an example of this grid array content?

  • I already updated the post

  • This ai is not a one-dimensional char array, it is an array of arrays, it will not work at all. You want to concatenate everything of this array into a string?

  • but it is possible to write a file with a multi-dimensional array?

  • I don’t know what you want to do with this data, there’s no way I can answer that. But if you want to join it all in one string, you need a loop

  • i re-edited the post with the desired result in the created file

  • And the size of this grid? Will it always be [4][2]? I think it is easier to save with a separator without converting to string, otherwise you will have the same job to read the file.

  • the size is set by the user each time the program is started, but after that, yes the size remains in the rest of the program.

  • So, maybe the best way to save is not the one you want, but using a separator, to make it easier to recover the data.

  • which means using a separator?

Show 5 more comments

1 answer

2


How it is an array that stores other arrays of the type char, this way will not work. You will need to turn the most internal arrays into string, then save to the file.

See your code in an example with modifications:

public static void Escreve() {

    char[][] grid = { { 'W', 'S', 'W', 'W' }, { 'W', 'W', '_', 'E' } };

    File newFile = <caminho do arquivo>;

    if (newFile.exists()) {

        System.out.println("já existe");

    } else {

        try {
            newFile.createNewFile();
            FileWriter fileW = new FileWriter(newFile);
            BufferedWriter buffW = new BufferedWriter(fileW);

            for (char[] g : grid) {
                buffW.write(String.valueOf(g) + System.getProperty("line.separator"));
            }

            buffW.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

The output in the text file is:

WSWW
WW_E

Notice that beyond the loop, I used System.getProperty("line.separator") for it to break line, as shown in your example, regardless of the operating system run.

  • 1

    That’s right, thanks for your help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.