我应该在何时何地使用close()方法来避免ObjectInputStream中的IOException?

Ber*_*ard 0 java sockets networking tcp objectinputstream

我正试图通过tcp从客户端程序中读取一个对象.正如您在此行中所看到的,我创建了objectInput:

ObjectInputStream objectInput = new ObjectInputStream(incoming.getInputStream());
Run Code Online (Sandbox Code Playgroud)

然后从其他程序中读取我的输入.它曾经工作正常,直到我做了一些小的改动来清理程序.个人假设我补充说

objectInput.clsoe();
Run Code Online (Sandbox Code Playgroud)

我的问题是,在读取对象后,我应该关闭objectInputStream还是保持不关闭?我应该在使用它之后立即关闭它还是在if块结束时或在程序结束时关闭它?关闭有什么影响?顺便说一句,我已经阅读了密切的文档.

这是错误:

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
    at Server.ClientWorker.run(MyCollectionServer.java:116)
    at java.lang.Thread.run(Thread.java:680)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try 
        {
            serverSocket = new ServerSocket(port);  
        } 
        catch (IOException e) 
                {
            e.printStackTrace();
        }
        while(true)
        {   
            ClientWorker w;
            try
            {   
                w = new ClientWorker(serverSocket.accept());
                Thread t = new Thread(w);
                t.start();
            }
            catch(IOException e)
            {
                e.printStackTrace();
                break;
            }   
        }
    }
}

class ClientWorker implements Runnable
{
.....
    private Socket incoming;
    public ClientWorker(Socket incoming)
    {
        myList = new ArrayList<PureAlbum>();
        loadList();
        this.incoming = incoming;
    }

.....
    public synchronized  void run()
    {

else if(request.compareTo("AddAlbum")==0)
        {
            try
            {
                ObjectInputStream objectInput = new ObjectInputStream(incoming.getInputStream()); //This is the line mentioned in the error
                PureAlbum object = (PureAlbum) objectInput.readObject();


                if(object instanceof CDAlbum)
                {
                    CDAlbum a = (CDAlbum) object;
                    myList.add(a);
                    System.out.println("Artist = " + a.getArtist());
                }
                else if(object instanceof Client.au.edu.uow.Collection.DVDAlbum)
                {
                    myList.add((DVDAlbum) object);              
                }
                else
                {
                    System.err.println("Warning : The object to add to database is unknown! "+ object.getClass() + "*");
                    System.exit(0);
                }


            }
            catch (UnknownHostException e) 
            {
                System.err.println("Can not read the host name");
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                System.err.println("Can not read the FILE name"); //This exception has been called
                e.printStackTrace(); 
            }
            catch (ClassNotFoundException e) 
            {
                e.printStackTrace();
            }
        }
Run Code Online (Sandbox Code Playgroud)

Ale*_*exR 5

你的代码片段很长,所以我会尝试给你一个通用的答案,它有望帮助你.

stream.close()java 7之前的典型使用模式是:

InputStream in = null; 
try {
    InputStream in = .....; // create your input stream;
    // use input stream
} catch (IOException e) {
    // do what you need here
} finally {
    if (in != null) {
        in.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

或者只是将方法声明为throws IOException然后写:

InputStream in = null; 
try {
    InputStream in = .....; // create your input stream;
    // use input stream
} finally {
    if (in != null) {
        in.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意此示例不包含catch部分.

从java 7开始,我们可以享受该语言的新功能:

try (
    InputStream in = .....; // create your input stream;
) {
    // use input stream
}
Run Code Online (Sandbox Code Playgroud)

你甚至根本不需要打电话close().定义到实现接口的try块头中的所有资源Closable将自动关闭.