trying to save/load files in java

Discussion in 'Mac Programming' started by stone315, Nov 18, 2008.

  1. stone315 macrumors regular

    Joined:
    Jun 17, 2008
    #1
    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.
     
  2. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
  3. stone315 thread starter macrumors regular

    Joined:
    Jun 17, 2008
    #3
    ^^ 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!
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
  5. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #5
    I swear your pager must goes off when someone posts one of these! :eek:
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    It just shows that I am super dull, and hit refresh whenever the commercials come on. =)

    -Lee
     
  7. stone315 thread starter macrumors regular

    Joined:
    Jun 17, 2008
    #7
    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?
     
  8. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #8
    Why not use a file selection control?
     

Share This Page