简体   繁体   中英

DataInputStream and DataOutputStream, compiles fine but does not run the way I wanted

I'm working on a program that takes user input in the form of a textfield and two comboboxes and displays the totals below. I have all of that working, but now I am trying to have the numbers selected saved and reread the next time the program is opened. I was lead to believe this is done with datainputstream and dataoutput stream.

I have coded both into my program and it compiles fine, but when I try to enter new data in, it catches it and closes (I coded the system.exit in to find out if its working or not).

I'm sure its something with my syntax, but I can't find it.

The whole program is here: http://pastebin.com/9L686Pxx

Edit: The formatting is a lot easier than I thought, so this is the block of code that is causing the program to exit.

try 
    {
            int economyCount = input.readInt();
            int standardCount = input.readInt();
            int advancedCount = input.readInt();
            int exceptionalCount = input.readInt();
            int redCount = input.readInt();
            int greenCount = input.readInt();
            int blueCount = input.readInt();
            int yellowCount = input.readInt();
    } 
    catch(IOException io)
    {
        JOptionPane.showMessageDialog(null, "The program could not read the data. Please check the disk drive and then run the program again.", "Error", JOptionPane.INFORMATION_MESSAGE);

        System.exit(1);
    }

You need to print out or log the stacktrace (or at least the error messages) for the exceptions that you are catching. Currently, your code is throwing away the evidence of what is causing the problem. (Hint: look at the javadoc for Exception.printStackTrace() .)

Alternatively, run your application using your IDE's debugger, and set a breakpoint on the System.exit call that is causing the application to exit. Then examine the exception to find its classname and message.

The chances are that this will give you enough evidence to allow you to identify and fix the root problem. If not, add the complete stacktrace to your Question.


Based on the fact that the exception occurred at that point, I suspect that the problem is that you are attempting to read data that hasn't been written yet. It looks like the sequence is:

  1. Open output ... which truncates the existing file.
  2. Open input
  3. Attempt to read 4 values from input . Ooops! Nothing there yet ... exception.

Once you have gotten past that, there are other problems with the way you are reading and writing:

  • Neither the read or write code seems to reset the data streams to the start.
  • The read phase is writing 4 ints and the write phase is writing 8 ints ... in a different order.

IMO, trying to reuse the same DataInputStream and DataOutputStream objects is a bad idea. You should recode it to, "open, read, close" and then "open, write, close" each time ... in the actionPerformed method. The input and output variables should be local variables, not instance variables.

The belated evidence of your stacktrace confirms this diagnosis.

If you're using DataInputStream.readFully() you need to catch EOFException separately. It means you've exhaused the input, so you should close it and stop reading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM