Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stone315

macrumors regular
Original poster
Jun 17, 2008
149
0
EDIT: I just realized my original post was mildly overwhelming, so if anyone can tell me how to write a list of booleans to a .txt file and then later read that file and get the list of booleans, I can figure the rest out on my own. Thank you so much!

Hi, I'm working on a lab for my computer science course and am having some difficulties attempting to save and load files. I've created an application that runs the game of life, and I'm attempting to create a function that saves the current pattern onscreen. My application is based off a two-dimensional array of cells that contain a boolean value specifying whether the cell is alive or dead. I'm trying to use a FileWriter to write all the boolean values to a .txt file, then use a FileReader to read the .txt file and set all the cells in the array to the boolean values saved in the .txt file. I'm really shaky on the whole FileWriter/FileReader concept because it's extra credit so my prof hasn't gone over it, so I'd really appreciate any ideas on how to code this.
 

stone315

macrumors regular
Original poster
Jun 17, 2008
149
0
^^ Awesome, thanks, didn't know how to do that. Anyways, the way I have it set up is as part of an ActionListener that's activated when the user presses a "save" button on the gui.

Code:
private class SaveListener implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
      Object source = event.getSource();
		
      if (source == saveButton)
      {
	boolean[][] lifeValues = game.convertToBooleans(); // creates the two-dimensional array of booleans based off the current boolean values for each cell
				
	String filename = JOptionPane.showInputDialog("Save As: ") + ".txt" // asks the user for a filename and converts it to a ".txt" format. Let me know if there's a nicer way of setting this up.
				
	try
	{

	  FileWriter saver = new FileWriter(filename);
	  BufferedWriter br = new BufferedWriter(saver);
	  PrintWriter printer = new PrintWriter(br);
					
          // writes the booleans to a .txt file. 
	  for (int row = 0; row < lifeValues.length; row++)
	    for (int col = 0; col < lifeValues[row].length; col++)
	      printer.print(lifeValues[row][col]);
					
	  printer.close();
	}
				
	catch (FileNotFoundException exception)
	{
	  JOptionPane.showMessageDialog(null, "Could not load file.");
	}
	catch (IOException exception)
	{
	  JOptionPane.showMessageDialog(null, "Could not load file.");
	}
      }
			
      else if (source == loadButton)
      {
	String filename = JOptionPane.showInputDialog("File to load:") + ".txt";  // again, converts a given filename to a .txt filename. I might get rid of the concatenation and just tell the user to type in a .txt name though.
		
	try
	{
	  FileReader fr = new FileReader(filename);
	  BufferedReader reader = new BufferedReader(fr);

          // This is where I need the most help. I don't really know how to read the booleans from the file and then convert them back to an array that I can use to reset the values in the individual cells.
	}
				
	catch (FileNotFoundException exception)
	{
	  JOptionPane.showMessageDialog(null, "Could not load file.");
	}
				
      }
    }
  }

Thanks so much for any help!
 

iShater

macrumors 604
Aug 13, 2002
7,027
470
Chicagoland

stone315

macrumors regular
Original poster
Jun 17, 2008
149
0
http://java.sun.com/javase/6/docs/api/java/io/RandomAccessFile.html

I just wrote up a little test using RandomAccessFile and it's methods writeBoolean(boolean) and readBoolean().

If you have an array with fixed dimensions, it should be pretty easy to just write a list of row*col booleans this way, and read them back in.

-Lee

Awesome, thank you so much, I've got it working. Now, for the load button, I want to display a JComboBox that has a list of all possible files. (Unless there's a better way to display the options.) Is it possible to create a new file with the list of all the filenames, or would it be better to create an ArrayList with all the filenames?
 

iShater

macrumors 604
Aug 13, 2002
7,027
470
Chicagoland
Awesome, thank you so much, I've got it working. Now, for the load button, I want to display a JComboBox that has a list of all possible files. (Unless there's a better way to display the options.) Is it possible to create a new file with the list of all the filenames, or would it be better to create an ArrayList with all the filenames?

Why not use a file selection control?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.