Best way to copy file from one location to another in Linux Server by java
Asked
Active
Viewed 123 times
1 Answers
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