FileInputStream alone VS BufferedInputStream wrapped FileInputStream

Discussion in 'Java' started by Krick, Aug 27, 2003.

  1. Krick

    Krick Guest

    I've been trying to settle on an implementation for a fileCopy method
    in one of my utility classes.

    After searching the newsgroups, I've narrowed it down to two
    implemenations (source for both are below). The first uses
    FileInputStream directly, the second wraps the FileInputStream in a
    BufferedInputStream. Both methods read in 8K chunks using their own
    internal buffers.

    My question is this:

    Which implemetation is better, and why?


    ....
    Krick





    public static void fileCopy1(String src, String dst) {
    try {
    FileInputStream in = new FileInputStream(new File(src));
    FileOutputStream out = new FileOutputStream(new File(dst));
    byte[] buffer = new byte[8 * 1024];
    int count = 0;
    do {
    out.write(buffer, 0, count);
    count = in.read(buffer, 0, buffer.length);
    }
    while (count != -1);
    in.close();
    out.close();
    } catch (IOException ex) {}
    }

    public static void fileCopy2(String src, String dst) {
    try {
    BufferedInputStream in = new BufferedInputStream(new
    FileInputStream(src));
    BufferedOutputStream out = new BufferedOutputStream(new
    FileOutputStream(dst));
    byte[] buffer = new byte[8 * 1024];
    int count;
    while ((count = in.read(buffer)) >= 0) {
    out.write(buffer, 0, count);
    }
    in.close();
    out.close();
    } catch (IOException ex) {}
    }
     
    Krick, Aug 27, 2003
    #1
    1. Advertising

  2. "Krick" <> wrote in message
    news:...
    > I've been trying to settle on an implementation for a fileCopy method
    > in one of my utility classes.
    >
    > After searching the newsgroups, I've narrowed it down to two
    > implemenations (source for both are below). The first uses
    > FileInputStream directly, the second wraps the FileInputStream in a
    > BufferedInputStream. Both methods read in 8K chunks using their own
    > internal buffers.
    >
    > My question is this:
    >
    > Which implemetation is better, and why?
    >


    Benchmarking the two will likely give you the answer that is better for your
    app. As far as one can see, the only difference is the buffering which is
    usually meant to improve performance.

    Of course, the output can be buffered as well.

    HTH,

    --
    Shripathi Kamath
    NETAPHOR SOFTWARE INC.
    http://www.netaphor.com
     
    Shripathi Kamath, Aug 27, 2003
    #2
    1. Advertising

  3. Jack9:

    >I have no idea and every
    >tutorial on socket programming seems to implement network connections
    >differently without explaining WHY they choose the streams they do.
    >Any help would be appreciated!


    Check out Roedy's overview at <http://mindprod.com/jgloss/io.html> and
    <http://mindprod.com/fileio.html>.

    You should also visit related terms like socket.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Aug 28, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jimmy Zhang

    reset issue in bufferedInputStream

    Jimmy Zhang, Oct 15, 2003, in forum: Java
    Replies:
    6
    Views:
    782
    Jimmy Zhang
    Oct 15, 2003
  2. Jimmy Zhang
    Replies:
    1
    Views:
    680
    Roedy Green
    Oct 31, 2003
  3. Marc Twain
    Replies:
    5
    Views:
    4,808
    Andrew Thompson
    Jan 15, 2004
  4. Daniel

    BufferedInputstream problem

    Daniel, Mar 9, 2005, in forum: Java
    Replies:
    7
    Views:
    1,082
    Rogan Dawes
    Mar 10, 2005
  5. Plotinus
    Replies:
    2
    Views:
    298
    Tad McClellan
    Dec 17, 2004
Loading...

Share This Page