0

Is it posibble to save class File object in other directory? I send this class File object from Client directory to server directory by objectOutputStream. My question is how to create in server directory this class File object.

File f=(File)objectInputStream.readObject();
Path path = FileSystems.getDefault().getPath(".").toAbsolutePath(); 
path1=Paths.get(path+name); //name is a directory for this special client name
File a= new File(path1.toString()); 
  • `file.createNewFile()`? Also see https://stackoverflow.com/questions/4645242/how-do-i-move-a-file-from-one-location-to-another-in-java – user Jun 05 '20 at 19:32

1 Answers1

1

That's not what File does.

File is, basically, a euphemism for 'String'. A file object contains 1 field, of type string, with the full path to the field. It represents 'a way to read that file' and not the file itself. If you try to serialize it via ObjectOutputStream, all that you're transferring is the file's name. Not the contents.

You're going to have to transfer the actual bytes across the wire if you want to transfer the contents.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • So Object Class File isn't a file. Right? –  Jun 06 '20 at 09:25
  • Depends on the definition of the word 'file'. Serializing it does not includes only the name of the file. Not the contents. Not the permissions. Not the timestamps. – rzwitserloot Jun 06 '20 at 11:57