Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

File Transfer In Java

Options
  • 20-02-2007 10:07am
    #1
    Registered Users Posts: 5,379 ✭✭✭


    Hi All,

    I need to transfer a file from one machine to another via an object stream. I've written a class which reads the file into an array of bytes, I then send this object via an object stream and can write it back to disk at the other end, it all works fine.

    I was just wondering is there any built in object to do this? for example I know for transfering images you can use an ImageIcon which is serilizable.


Comments

  • Registered Users Posts: 443 ✭✭maceocc2


    yea dont bother breaking it into bits/bytes just create a new:

    File file1 = new File(filename);
    Object object1 = new Object(file1);

    then the object is serializable


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    Hi,

    I thought the File Object is just a link to the file, ie it's Path & Name, but not actually the data from the file, i could be wrong.


  • Registered Users Posts: 443 ✭✭maceocc2


    No it's definitly the data from the file. Its the same as ImageIcon and all that stuff.

    rite a simple program just to make sure if you want, Do the above lines then try and output the byte values to the command prompt or something.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    maceocc2 wrote:
    yea dont bother breaking it into bits/bytes just create a new:

    File file1 = new File(filename);
    Object object1 = new Object(file1);

    then the object is serializable
    For an object to be serializable, it has to implement the java.io.Serializable interface, which File does. Object does not, so not all objects are serializable.

    Also,
    Javadoc wrote:
    An abstract representation of file and directory pathnames.


  • Registered Users Posts: 443 ✭✭maceocc2


    This is correct, ObjectOutputStream is implemented in java.io.Serializable, so only objects that support the java.io.Serializable interface can be written to streams. So use something like this to do it,

    FileOutputStream fos = new FileOutputStream("t.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);


  • Advertisement
  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Hi,

    I thought the File Object is just a link to the file, ie it's Path & Name, but not actually the data from the file, i could be wrong.

    Your correct, the File object is just a reference to the file. It doesn't contain any data at all. You normally have to hook a stream to it.

    Look into SocketFactory for transferring the data over the network.
    import javax.net.*;
    
    SocketFactory socketFactory = SocketFactory.getDefault();
    
    Socket s = socketFactory.createSocket(host,port);
    

    Sending the data as an object is fine but if its large amount of data you could be wasting memory or causing lag in the transfer. This way sends data straight from file over the network into a receiving stream that writes directly to disk.


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    Hobbes wrote:
    Your correct, the File object is just a reference to the file. It doesn't contain any data at all. You normally have to hook a stream to it.

    Look into SocketFactory for transferring the data over the network.
    import javax.net.*;
    
    SocketFactory socketFactory = SocketFactory.getDefault();
    
    Socket s = socketFactory.createSocket(host,port);
    

    Sending the data as an object is fine but if its large amount of data you could be wasting memory or causing lag in the transfer. This way sends data straight from file over the network into a receiving stream that writes directly to disk.


    Hi Hobbes,

    Didn't know about socketFactory, will look into it.. the files aren't too large and it seems to be working well at the min...

    Yea that's what i thought about the file object. It would be vaild if you sent it to another process on the same machine, as it can access the file system to get the file, but not if you send it to another machine...


Advertisement