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?
– user28595
I already updated the post
– MDordio
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?
– user28595
but it is possible to write a file with a multi-dimensional array?
– MDordio
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
– user28595
i re-edited the post with the desired result in the created file
– MDordio
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.
– user28595
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.
– MDordio
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.
– user28595
which means using a separator?
– MDordio