简体   繁体   English

DataInputStream和DataOutputStream,可以正常编译,但不能按照我想要的方式运行

[英]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. 导致我相信这是通过datainputstream和dataoutput流完成的。

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). 我已经在程序中都进行了编码,并且可以正常编译,但是当我尝试输入新数据时,它将捕获并关闭(我对system.exit进行了编码,以查看其是否有效)。

I'm sure its something with my syntax, but I can't find it. 我可以用我的语法确定它,但是找不到。

The whole program is here: http://pastebin.com/9L686Pxx 整个程序在这里: 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() .) (提示:在Javadoc中查看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. 或者,使用IDE的调试器运行应用程序,然后在System.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. 打开output ...会截断现有文件。
  2. Open input 打开input
  3. Attempt to read 4 values from input . 尝试从input读取4个值。 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. 读取阶段正在写入4个整数,而写入阶段正在写入8个整数……以不同的顺序。

IMO, trying to reuse the same DataInputStream and DataOutputStream objects is a bad idea. IMO,尝试重用相同的DataInputStreamDataOutputStream对象是一个坏主意。 You should recode it to, "open, read, close" and then "open, write, close" each time ... in the actionPerformed method. 您应该每次在actionPerformed方法中将其重新编码为“打开,读取,关闭”,然后每次“打开,写入,关闭”。 The input and output variables should be local variables, not instance variables. inputoutput变量应该是局部变量,而不是实例变量。

The belated evidence of your stacktrace confirms this diagnosis. 您堆栈跟踪的最新证据证实了这一诊断。

If you're using DataInputStream.readFully() you need to catch EOFException separately. 如果使用的是DataInputStream.readFully() ,则需要分别捕获EOFException It means you've exhaused the input, so you should close it and stop reading. 这意味着您已经用尽了输入,因此您应该关闭它并停止阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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