objectinputstream

How do I save and multiple objects from a single file?

这一生的挚爱 提交于 2019-12-11 18:04:22
问题 I'm trying to create a system where a user can choose a set of ID's(Federation IDs) to retrieve data from an external API. After the data is retrieved from the web service I wish to store it locally for later use. Now, my problem is that I know how to load an object from an ObjectInputStream by taking the entire file(federations.dat). Is there a way for me to load say "object WHERE id = N" from "federations.dat" ? Or do I have to create separate files for each object? This is my load method:

Client - Server ObjectInputStream Error

戏子无情 提交于 2019-12-11 06:45:45
问题 my code is this try { //Connect to the login server socket = new Socket(hostName , 15000); out = new ObjectOutputStream(socket.getOutputStream()); input = new ObjectInputStream(socket.getInputStream()); .. //perform the login , if the login succeed make answ=1; } catch (UnknownHostException e) { System.err.println("Unknown host: " + hostName); StatusLabel.setText("Unknown host"); } catch (ConnectException e) { System.err.println("Connection refused by host: " + hostName); StatusLabel.setText(

Reading Objects until End of File in java

烈酒焚心 提交于 2019-12-11 04:49:27
问题 I'm trying to write a program where the user can: 1) Add a person to the contact (name, phone, email), 2) Remove a person from the contacts, 3) Read all from contact. The Way I'm doing this is I'm asking for the user for their choice and respectively does whatever. For writing, I simply write an object to the file. For removing, I think I'll be asking the user for "last name" which will be used as the KEY (since I'm using a TreeMap)and will remove the value (object) at the key. So I'm having

Deserializing multiple objects from file without using while(true)

雨燕双飞 提交于 2019-12-11 02:38:42
问题 I have a block of code, that deserializes multiple objects from file. How can i avoid using a while(true)? ObjectInputStream in = new ObjectInputStream(new FileInputStream( filename)); while (true) { try { MyObject o = (MyObject) in.readObject(); // Do something with the object } catch (EOFException e) { break; } } in.close(); 回答1: You should write either a collection (with a size), or a put a marker before each object: try { for (;in.readBoolean();) { MyObject o = (MyObject) in.readObject();

ObjectInputStream read a empty object

爱⌒轻易说出口 提交于 2019-12-11 01:33:41
问题 I wrote a object using ObjectOutputStream, and read it using ObjectInputStream, and tested it , I get the expected result. But when write the object in other machine, read it in my computer, the read object's members are empty. Could someone help me? thanks public class TrustNet implements Serializable{ public double[][] trusts; public double avg = 0; public TrustNet(int size){ trusts = new double[size][size]; } public void writeToFile(String fileName){ try(ObjectOutputStream writer = new

Program pauses on initializing object input stream in Java

十年热恋 提交于 2019-12-10 20:09:05
问题 While running debugger, the program pauses on initializing object streams from server main input output streams. Following is the code : public TFileReader(Client cli)throws Exception{ this.cli = cli; fileSock = new Socket(cli.ServerIp(), cli.FilePort()); fobjIn = new ObjectInputStream(fileSock.getInputStream()); fobjOut = new ObjectOutputStream(fileSock.getOutputStream()); fobjOut.flush(); } @Override public void run(){ try{ System.out.println("file reader thread online"); fobjOut

Persisting B-Tree nodes to RandomAccessFile

大兔子大兔子 提交于 2019-12-08 06:58:33
问题 My project is trying to write a B-Tree. I am having trouble persisting nodes of the tree to a random access file. I am constantly encountering EOFexceptions and StreamCorruptionExceptions . some resources that I am currently using are: Converting any object to a byte array in java https://inaved-momin.blogspot.com/2012/08/understanding-javaiostreamcorruptedexce.html current Problem: reading a link position from a node and then trying to read it from the random access file is causing a

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

Receive an object over TCP/IP

杀马特。学长 韩版系。学妹 提交于 2019-12-04 03:58:56
问题 I am going to write a program over TCP/IP and I should send objects by client or by server, It is going right when I want to send or receive strings but when I am trying to read an object: private Socket client; public ThreadedClient(Socket client) { this.client = client; } @Override public void run() { try { ObjectInputStream objIn = new ObjectInputStream(client.getInputStream()); while(true){ try { Object fromClient = objIn.readObject(); } catch (ClassNotFoundException e) {e.printStackTrace

java.net.SocketTimeoutException: Read timed out

亡梦爱人 提交于 2019-12-03 13:10:26
I have an application with client server architecture. The client use Java Web Start with Java Swing / AWT and the sert uses HTTP server / Servlet with Tomcat. The communication is made from the serialization of objects, create a ObjectOutput serializes a byte array and send to the server respectively called the ObjectInputStream and deserializes. The application follows communicating correctly to a certain time of concurrency where starting to show error "SocketException read timeout". The erro happens when the server invoke the method ObjectInputStream.getObject() in my servlet doPost method