objectoutputstream

EOFexception in Java when reading objectinputstream

人走茶凉 提交于 2019-12-08 08:06:00
问题 I want to read multiple objects (my own class Term) that I have output to a .dat file, but I always get a nullPointException or EOFException. ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(masterFile)); Object o = null; while(( o = inputStream.readObject()) != null){ Term t = (Term)o; System.out.println("I found a term"); } 回答1: See the Javadoc. readObject() doesn't return null at EOF. It throws EOFException. The only way it can return a null is if you wrote a null

ObjectOutputStream hanging forever

╄→尐↘猪︶ㄣ 提交于 2019-12-08 03:52:37
问题 I have a client that connects to a server using SSLSocket. Next, I try to create an OOS with ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream()); If everything is working well on the server side, this is fine. However, I would like to put something in place on my client side that tries to create the ObjectOutputStream, but if it hasn't happened in 60 seconds, log the error and continue processing. I don't see any timeout options for this. Any examples of how this

EOFexception in Java when reading objectinputstream

落爺英雄遲暮 提交于 2019-12-07 23:35:29
I want to read multiple objects (my own class Term) that I have output to a .dat file, but I always get a nullPointException or EOFException. ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(masterFile)); Object o = null; while(( o = inputStream.readObject()) != null){ Term t = (Term)o; System.out.println("I found a term"); } See the Javadoc. readObject() doesn't return null at EOF. It throws EOFException. The only way it can return a null is if you wrote a null at the other end, and that's not necessarily a good reason for terminating the read loop. In short your code

writing a BitSet to a file in java

Deadly 提交于 2019-12-07 01:45:20
问题 I have a BitSet and want to write it to a file- I came across a solution to use a ObjectOutputStream using the writeObject method. I looked at the ObjectOutputStream in the java API and saw that you can write other things (byte, int, short etc) I tried to check out the class so I tried to write a byte to a file using the following code but the result gives me a file with 7 bytes instead of 1 byte my question is what are the first 6 bytes in the file? why are they there? my question is

JSONObject Not Serializable?

不问归期 提交于 2019-12-06 23:15:46
问题 I am trying to serialize an ArrayList of JSONObjects. But I get the error: 05-07 01:04:24.130: WARN/System.err(913): java.io.NotSerializableException: org.json.JSONObject 05-07 01:04:24.130: WARN/System.err(913): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535) 05-07 01:04:24.130: WARN/System.err(913): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847) 05-07 01:04:24.130: WARN/System.err(913): at java.io.ObjectOutputStream.writeObject

Android Socket + ObjectOutputStream not working correctly

限于喜欢 提交于 2019-12-05 04:36:46
Hey guys. I am developing a client/server program where the client is an android device. The server has a listener class that reads an object from the input stream. I created a client software for another COMPUTER that sends a small object over a local network. Computer to Computer works perfectly fine , i read the object and printed the contents. However, the SAME code ported over to android (I rewrote it just in case) does not work. I construct an object (ANY object), which is serializable, and send it via the ObjectOutputStream. My server running on the computer does connect to the device,

JSONObject Not Serializable?

ぃ、小莉子 提交于 2019-12-05 02:22:59
I am trying to serialize an ArrayList of JSONObjects. But I get the error: 05-07 01:04:24.130: WARN/System.err(913): java.io.NotSerializableException: org.json.JSONObject 05-07 01:04:24.130: WARN/System.err(913): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535) 05-07 01:04:24.130: WARN/System.err(913): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847) 05-07 01:04:24.130: WARN/System.err(913): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689) 05-07 01:04:24.130: WARN/System.err(913): at java.io.ObjectOutputStream

ObjectInputStream readObject(): ClassNotFoundException

倾然丶 夕夏残阳落幕 提交于 2019-12-04 04:31:55
问题 In both client and server classes, I have an exact same inner class called Data. This Data object is being sent from the server using: ObjectOutputStream output= new ObjectOutputStream(socket.getOutputStream()); output.writeObject(d); (where d is a Data object) This object is received on the client side and cast to a Data object: ObjectInputStream input = new ObjectInputStream(socket.getInputStream()); Object receiveObject = input.readObject(); if (receiveObject instanceof Data){ Data

What's the difference between Serialization and simply store the object on disk?

风格不统一 提交于 2019-12-03 22:19:43
I am confused about this. Since when doing implementation of Serializable class, we need to use classes like FileOutputStream , ObjectOutputStream or something like that. Then why not we just use those classes to do things like output a object to a file and input a object from a file to maintain the status of a object directly? Why should we first implement Serializable and then do the same thing? Understand it like this... Serializable is marker interface which denotes that the object of your class can be converted into byte stream and eventually back into java object if required. Initially

How can I convert an Object to Inputstream

╄→尐↘猪︶ㄣ 提交于 2019-12-03 19:10:57
问题 How can I convert the java Object into a InputStream? 回答1: You can use ObjectOutputStream You write the object (obj in the code below) to the ObjectOutputStream, your object you want to convert to an input stream must implement Serializable. ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(obj); oos.flush(); oos.close(); InputStream is = new ByteArrayInputStream(baos.toByteArray()); 来源: https://stackoverflow.com