-3

Best way to copy file from one location to another in Linux Server by java

SR Ranjan
  • 113
  • 1
  • 5
  • https://stackoverflow.com/questions/4004760/fastest-way-to-copy-files-in-java -- duplicate – z21 Jun 29 '17 at 10:01

1 Answers1

0
private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
    is = new FileInputStream(source);
    os = new FileOutputStream(dest);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = is.read(buffer)) > 0) {
        os.write(buffer, 0, length);
    }
} finally {
    is.close();
    os.close();
}

}

Nithees balaji
  • 148
  • 1
  • 11
  • you should use try with resources or at least check before closing the streams. If you pass in a non existent input file, creating the FileInputStream will throw an IOException, and in the finally block by calling close() you'll get a NullPointerException – P.J.Meisch Jun 29 '17 at 14:22