0

I've a client and a server. The server maintains a File and the client does a request for it. So, what I do is

File CONFIGURATION_FILE=new File(configuration.doc);
System.out.println (CONFIGURATION_FILE.exist()); //return true

in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
oos=new ObjectOutputStream(socket.getOutputStream());

String line;
while ((line=in.readLine())!=null) {

  if (line.equals("configuration file"))
        oos.writeObject(CONFIGURATION_FILE);
        oos.flush();
  }
}

When I receive on the client the File, it throws an IOException. The program works if I pass the absolute path when I create the File (client and server are on the same machine for the moment, so I think that what is passed through the socket is a reference to the path and both the client and the server can see that). My question is: Is possible sending a File as an Object through the socket channel directly or I must convert it to an array of bytes and send this array? Thank you in advance

2
  • 2
    The File object is an "abstract representation of file and directory pathnames" (javadoc). So no, you can't just send it through a socket. You can hovewer use NIO to simplify the handling of the streams. Commented Jul 13, 2015 at 10:42
  • 2
    File really should have been called Path, but it's too late for them to fix it now. Commented Jul 13, 2015 at 10:48

1 Answer 1

0

You need to convert it to bytes and send bytes. For example your Server may have the file in F:/Out.txt so if you send the actual file object to a client, the client might not have a F: drive at all. Furthermore the File class makes use of a lot native methods. Serializing and De-serializing it is never a good idea.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.