Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+15 votes
asked in CSC305 Fall 2022 by (1 point)

1 Answer

+5 votes

First, if you're asking about how to open dialog boxes to allow the user to select a file name to open or to save to, then simply take a look at this FileChooser class. But that only gets you the name and path of the file they'd like to open or save.

I'm assuming you're really wondering about the actual process of opening or saving the tile elevation data to a file.

I plan to talk about this more in class later (probably during Sprint 2), but the short answer is that IF you set up your Java classes using a good MVC architecture (separating your data M-odel from your V-iew and C-ontroller), then this can be quite easily accomplished by "serializing" your model classes into a text-based format such as JSON, using a 3rd party library like gson.

Of course, without learning that fancier stuff, it would also be possible to come up with your own proprietary text file format: maybe starting with something simple like:

[grid-width W]
[grid-height H]
[elevation of tile 0 0]
[elevation of tile 1 0]
[elevation of tile 2 0]
...
elevation of tile (W-1) (H-1)

Then you could simply use a java.util.Scanner to read in from the file you open, and a PrintWriter to write out the data when save to it.

answered by (5.7k points)
...