简体   繁体   English

为什么我的程序停止运行并且不返回错误?

[英]Why does my program stop running and does not return error?

I put the declaration in the while loop, and the program would not running and also does not return any error. 我将声明放入while循环中,该程序无法运行,也不会返回任何错误。 I suspect the while loop become an infinite loop. 我怀疑while循环会变成无限循环。

                    try
                    {
                        while (true)
                        {
                            inputStream = new ObjectInputStream (new FileInputStream (fileName));
                            Ship copyObject = (Ship) inputStream.readObject();
                            String nameCompany = copyObject.getCompanyName();

                            if (compName.equalsIgnoreCase(nameCompany)){
                                listShipName += (copyObject.getShipName() + ", ");
                                numberOfShip ++;
                            }
                        }
                    }

                    catch (EOFException e)
                    {
                    }

                    catch (Exception e)
                    {
                    e.printStackTrace();
                    }

But if I put the declaration of input stream out of the while loop, the program runs successfully. 但是,如果我将输入流的声明置于while循环之外,则程序将成功运行。 Can someone explain why this happens? 有人可以解释为什么会这样吗?

                        try
                        {
                            inputStream = new ObjectInputStream (new FileInputStream (fileName));

                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                        try
                        {
                            while (true)
                            {

                                Ship copyObject = (Ship) inputStream.readObject();
                                String nameCompany = copyObject.getCompanyName();

                                if (compName.equalsIgnoreCase(nameCompany)){
                                    listShipName += (copyObject.getShipName() + ", ");
                                    numberOfShip ++;
                                }
                            }
                        }

                        catch (EOFException e)
                        {
                        }

                        catch (Exception e)
                        {
                        e.printStackTrace();
                        }

You're reopening your file on every iteration through the loop, which means you are only ever reading the first object from the file. 您将在循环中的每次迭代中重新打开文件,这意味着您仅从文件中读取第一个对象。 But you're reading the same object over and over again. 但是,您一次又一次地读取同一对象。

As well as opening your file only once, you really should try to detect the end of file without throwing an exception. 除了只打开文件一次之外,您实际上应该尝试检测文件的结尾而不会引发异常。 As a matter of style, exceptions should be thrown when things go wrong, not as a matter of course. 作为样式问题,应该在出现问题时引发异常,而不是理所当然的事情。

现在,我意识到在每次迭代中,我都会重新打开输入流,因此循环不会到达文件的末尾,并且循环变为无限。

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

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