Results 1 to 6 of 6
  1. #1
    sayan751 is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Question how to handle files?

    hi,
    i dealt with ".txt" files in java.
    can some one please tell me how to deal with binary files(such as image or audio or a office document etc.) or at least refer some good tutorial. i searched the net, all they says about streaming.what the streaming is all about?
    i am waiting for a brief and resourceful tutorial.
    thanx

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Depends what the file is and what you want to do with it. Image? Audio? Office document?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    sayan751 is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    thanx for your reply.

    actually i intend to upload a file from a client machine to server, and i have worked with ".txt" files(using the readLine() method to read from the source file and write it down in another .txt file in the server).

    but the same can't be applied to a image file or a audio file(i tried to do but it is not useful). so my question is what is the idea behind general file handling?

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Well, you could read them as ImageIcon's or the audio equivalent (Sound API is not my strong point) and send/read those using Object streams, but I'm not sure how you would go about writing them to the server.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    For general binary files, the easiest approach is to use FileInputStream and FileOutputStream.

    For example, the following will copy from one file to another:

    Java Code:
    FileInputStream in = new FileInputStream(new File("inFile.bin"));
    FileOutputStream out = new FileOutputStream(new File("outFile.bin"));
    
    byte[] bytes = new byte[65536];
    
    try {
    
    while(true) {
      int numRead = in.read(bytes);
    
      if(numRead == -1)
        break;
    
      out.write(bytes, 0, numRead);
    }
    
    } catch (IOException ioEx) {
    ... do something
    } finally {
      try {
        in.close();
      } catch (Throwable t) {}
    
      try {
        out.close();
      } catch (Throwable t) {}
    }

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Toadaly is right... use File streams, not object streams. Forgot that File streams existed for a bit there... So tired :(
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. How to handle socket Exception
    By mayank0512 in forum Networking
    Replies: 14
    Last Post: 12-22-2010, 12:31 AM
  2. handle FileNotFoundException
    By minifish in forum New To Java
    Replies: 13
    Last Post: 11-08-2008, 03:01 AM
  3. Handle exception manually
    By John_28 in forum New To Java
    Replies: 2
    Last Post: 06-05-2008, 12:26 PM
  4. Better way to handle exceptions
    By javaplus in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2008, 07:47 PM
  5. how to handle exceptions
    By paty in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 05:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •