天道酬勤,学无止境

objectoutputstream

更改实例状态不会反映在序列化对象中(Changing instance state does not get reflected in serialized object)

问题 我写了以下简单的代码 public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("data.txt"))); Human human = new Human(); human.setAge(21); human.setName("Test"); System.out.println("Human : " + human); oos.writeObject(human); human.setName("Test123"); oos.writeObject(human); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("data.txt"))); Human newHuman1 = (Human)ois.readObject(); System.out.println("newHuman1 :" + newHuman1); Human newHuman2 = (Human

2021-06-21 09:20:03    分类:技术分享    java   serialization   deserialization   objectinputstream   objectoutputstream

writing a BitSet to a file in java

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 relevant to a BitSet because i don't want to start writing lots of data to a file and realize I have random

2021-06-14 21:08:22    分类:问答    java   bitset   objectoutputstream

Android Socket + ObjectOutputStream not working correctly

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, but it

2021-06-14 15:25:50    分类:问答    java   android   sockets   objectoutputstream   classnotfoundexception

SocketException:使用 ObjectInputStream 在服务器上重置连接(SocketException: Connection reset on server with ObjectInputStream)

问题 我试图了解ObjectInputStream/ObjectOutputStream ,所以我创建了一个非常简单的服务器-客户端应用程序,其中客户端通过创建的流发送一个 HashMap 对象,服务器接收它并将其打印出来。 这是我的服务器代码: import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws IOException, ClassNotFoundException { ServerSocket server = new ServerSocket(4444); while (true) { Socket socket = server.accept(); ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream()); if (objIn.readObject() != null) { System.out.println(objIn.readObject()); } } } } 这是我的客户端代码: import java.io.*; import java.net.InetAddress; import java.net.Socket

2021-06-10 19:28:29    分类:技术分享    java   sockets   client-server   objectinputstream   objectoutputstream

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

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?

2021-06-03 10:01:22    分类:问答    java   serialization   serializable   fileoutputstream   objectoutputstream

What's the difference between writeUTF and writeChars?

What's the difference between writeUTF and writeChars? (methods of ObjectOutputStream) Further I have not found the corresponding readChars in ObjectInputStream.

2021-06-02 08:42:34    分类:问答    java   io   objectoutputstream

How to store a class object into Internal Memory Storage using serializable?

I need to store this object into the internal storage memory of the phone, and i have the <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"></uses-permission> set on the manifest. The object itself haves two static methods for store and reload it from the internal memory: public class SaveState implements Serializable { static public List<FullMagazine> fms = new ArrayList<FullMagazine>(); static SaveState instance=null; public static SaveState getInstance(){ if( instance == null ) instance = new SaveState(); return instance; } public static void saveData(SaveState

2021-05-25 09:52:44    分类:问答    java   android   serialization   objectoutputstream

将Javas对象流与套接字一起使用时出现性能问题(Performance issue using Javas Object streams with Sockets)

问题 我正在尝试使用Java中的套接字和对象流来进行本地IPC,但是性能却很差。 我正在测试通过ObjectOutputStream发送对象到通过Socket通过ObjectInputStream接收回复的ping时间。 这是请求者: public SocketTest(){ int iterations = 100; try { Socket socket = new Socket("localhost", 1212); ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream()); ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); double start = System.currentTimeMillis(); for (int i = 0; i < iterations; ++i) { Request request = new Request(); objectOutputStream.writeObject(request); Response response = (Response)objectInputStream

2021-05-25 01:23:33    分类:技术分享    java   performance   sockets   objectoutputstream   objectinputstream

Java ObjectOutputStream and updating a file

I am having trouble figuring out one implementation problem, I have one class, it behaves like list but instead of holding a file in some collection it saves them on a disk. The problem occurs when I want to add some element to my list. At the start of my file I have one int that tells me how many objects there are in my list, but I can't figure out elegant way to update this value. I have something like this: public boolean add(T element) { try { out.writeObject(element); out.flush(); //and here we need to update the int in my file } catch (IOException e) { e.printStackTrace(); } return true

2021-05-13 16:15:56    分类:问答    java   objectoutputstream

SocketException: Connection reset on server with ObjectInputStream

I'm trying to get my head around the ObjectInputStream/ObjectOutputStream, so I create a very simple server-client application where the client sends a HashMap object over the created stream and the server receives that and prints it out. This my server code: import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws IOException, ClassNotFoundException { ServerSocket server = new ServerSocket(4444); while (true) { Socket socket = server.accept(); ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream()); if (objIn.readObject() !=

2021-05-13 10:03:03    分类:问答    java   sockets   client-server   objectinputstream   objectoutputstream