Pages

Friday, April 29, 2011

Java -=- How to copy file using Apache IOUtils

Here is a code snippet that explain how to copy a file using IOUtils. You can get Apache IOUtils at http://commons.apache.org/io/download_io.cgi

InputStream in = null;
OutputStream out = null;

try {
    // Ensure folder is created
    FileUtils.forceMkdir(new File("/Users/yann/myfolder"));

    in = new FileInputStream(new File("/Users/yann/from.txt"));
    out = new FileOutputStream(new File("/Users/yann/myfolder", "to.txt"));

    IOUtils.copy(in, out);

} finally {
    IOUtils.closeQuietly(in);
    IOUtils.closeQuietly(out);
}

1 comment:

  1. Thanks for the example. This is the simplest, most direct example I have ever seen... I like it that way.

    ReplyDelete