basically i have a 3d array of chars, i need to be able to save and load it using text files.

Any as to how i would actually do it? Im familiar with reading lines using BuffererdReader and stuff.

Im thinking use a loop to save each element to a file. But how to get it to load. How do i split up each char so they each get read back into the correct elements?

9 Years
Discussion Span
Last Post by ~s.o.s~

Yeah i know how to read/write to text files, its jus the logic of actuaally getting it to read the line, split it up into chars, then store each char in a diffrent element, then move into the second line (second row in the array) and loop


If they are rectangular arrays and not jagged, meaning the number of elements in each dimension remains constant, you can simply stripe them into lines with a plain old loop and read them back out by the dimension offsets.
This array:
Can be written like this:
And can be reconstructed with a row offset of three.

If your dimensions vary, you may need to write the offsets into the file as well (i.e. on the first line)


I will try that

Its a rectangular array (SIZE by SIZE , where SIZE is a constant)


You mentioned a 3D array in the original post. If it's just a 2D rectangular array, you don't even need an offset. The offset is only needed for 3D (or greater) arrays.


Since a 2D char array is nothing but an array of char arrays, you can convert the char array to a String using String.valueOf() and write it out to a file using PrintWriter . When reading in data, just read in lines of data and recreate your 2d char array using String.toCharArray() .

Another interesting thing you can try out is to write out Java arrays as JSON which will render it usable by any application without having a proprietary format for your text file.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.