Can't save data by letter in Java

Discussion in 'Mac Programming' started by jsmwoolf, Aug 24, 2011.

  1. jsmwoolf macrumors regular

    Joined:
    Aug 17, 2011
    #1
    I'm trying to save numbers based off of letters. For example, A would store 2, B would store 4, C would store -3, etc. However, it appears that it can only save one variable at a time. I posted the writing section of the code.
    Code:
    	void writeDataVariable(double number, int letter) //For Variable writing purposes
    	{
    		try
    		{
    		System.out.println("Writing Data");
    		FileOutputStream outputStream = new FileOutputStream(data);
    		//Writes to the Variable
    		write(outputStream, Character.toString((char)(65+letter)) + "=" + Double.toString(number));
    		System.out.println("Done! Writing successful to Variable " + Character.toString((char)(65+letter)));
    		} catch (IOException ioe)
    		{
    			System.out.println("Error: " + ioe.getMessage());
    		}
    	}
    	
    	void write(FileOutputStream stream, String output)
    			throws IOException {
    				output = output + "\n";
    				byte[] data = output.getBytes();
    				stream.write(data, 0, data.length);
    			}
    The File declaration is not listed here but is first declared null and then initialized on the constructor.
    By my guess, it's the section:
    Code:
    write(outputStream, Character.toString((char)(65+letter)) + "=" + Double.toString(number));
    Even though it writes to a specific letter, it tends to always to overwrite the letter and that function and puts in its own number. How do I regulate each section without overriding the letter that already has a number in it? If you didn't understand, I'll explain it. Let's say you wanted to save a number to variable A. When you click on it, it would look like this on the document:

    A=4

    and you wanted to save another number to variable B. When you click on it, it'll override A and it would look like this:

    B=3

    When I launch the application, it'll show me this:

    B=3

    But Variable A is gone.
     
  2. jpyc7 macrumors 6502

    Joined:
    Mar 8, 2009
    Location:
    Denver, CO
    #2
    I'm not really understanding the problem, but I think you are saying that the user interacts with the program and the result is that the file contains "A=4".

    Then the user interacts with the program again and the result is that the file contains "B=2".

    But you wanted the file to contain 2 lines: "A=4\nB=2".

    If that is the case, I think you need to make sure that you are either not opening and closing the file between the writes. More importantly, you need to make the file open in "append" mode. I'm guessing this is C++ language.
     
  3. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #3
    This is Java. It even says on the Title of this thread. Besides, how can it overwrite a variable stored in A with a variable in B if they're not even the same letter? It should be adding, not overriding.
     
  4. jpyc7 macrumors 6502

    Joined:
    Mar 8, 2009
    Location:
    Denver, CO
    #4
    Ha, I missed that. And I even write Java at work.

    Well, my suggestion still stands. The FileOutputStream can take a second parameter (boolean) indicating that you want to append.
     
  5. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #5
    Oh, Okay. Now it works just as I want it. Should have done research on it and its arguments.

    Thanks
     

Share This Page