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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

File Transfer In Java

  • 20-02-2007 9:07am
    #1
    Registered Users, Registered Users 2 Posts: 5,401 ✭✭✭


    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, Registered Users 2 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, Registered Users 2 Posts: 5,401 ✭✭✭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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 5,401 ✭✭✭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