Is java's File class' object serializable?
Asked Answered
L

1

6

I am working on a project, it requires to send directory information from one client to another on a network. so tell me, can i get this information on another client by only sending File class' object? please explain me the reason for your YES or NO.

Updated Question

i am posting the code which i have tried....

The Client Code:-

public class FileClient{
    public static void main(String str[]){
        try{
            Socket sock = new Socket("10.16.10.82",4000);

                    /* Socket sock = new Socket("127.0.0.1",4000); */ //for localhost 
            File f = new File("MyFile.txt");
            ObjectOutputStream os = new ObjectOutputStream(sock.getOutputStream());
            os.writeObject(f);
            os.flush();
            os.close();
            System.out.println("object sent");
        }
        catch(Exception ex){
            ex.printStackTrace();

        }

    }
}

And The following is the server code:-

class FileServer{
    public static void main(String str[]){
        try{
            ServerSocket sock = new ServerSocket(4000);
            while(true){
                Socket cs = sock.accept();
                ObjectInputStream in = new ObjectInputStream(cs.getInputStream());
                File f = (File)in.readObject();
                System.out.println(f.isDirectory());
                System.out.println(f.length()+" bytes read");
            }
        }
        catch(Exception ex){
            ex.printStackTrace();

        }

    }
}

this works fine if i run server on localhost but when i use two different machines one for client and other for server the output is different (it shows 0 bytes read). it seems right because the actual file is not available at remote server. i am confused about File class' field i.e.,

static private FileSystem fs = FileSystem.getFileSystem() //see javadoc for File

what exact information server is getting after reading the File object?

Licha answered 1/8, 2014 at 5:31 Comment(10)
Based on the JavaDocs for File, yes. Having said that, I would greatly discourage sending File over the wire as it is contextual to the local machine. It would be better to devise your own class which carried the information you need - IMHOGorgonian
Also, why didn't you just to and use a ObjectOutputStream and try writing a File object out to disk and see what happened?Gorgonian
thanks a lot, i will definitely try this. why would not i thought that :-)Licha
More to the point, why didn't you look in the Javdoc? It's pointless asking Internet questions on matters that are already documented. Just a complete waste of time.Mattock
FYI, a File object is little more than a thin wrapper around a file path.Changeful
@Gorgonian i tried writing a 'File' object to another client using 'ObjectOutputStream' but it didn't work.... it looks i have to make my own class.Licha
@Licha Really, I did a small write test it worked fine for me...Gorgonian
@EJP you didn't read the details i provided.... i read the java docs before posting the question. Every question is worthwhile, whether it's a silly one.Licha
@Licha And I was able to read it. Don't know if that means it will go over the wire though...Gorgonian
i have edited the question with code.... please go through it.Licha
A
3

Yes you can send information using the File class reference because it implements the Serializable interface.

For more details, read Java's File API reference.

Code example

You can achieve it by following code:

String sourcePathFilename = "path.txt";
try {
    File file = new File(sourcePathFilename);
    FileOutputStream fos = new FileOutputStream(String targetpath);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(file);
} catch (Exception e) {
    e.printStackTrace();
}
Atbara answered 1/8, 2014 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.