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?

Recommended Answers

All 8 Replies

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:
123
456
789
Can be written like this:
123456789
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.

yeah sorry i meant 2D i got confused

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.