loisduplain

Copy file in Java

Jan 27th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.46 KB | None | 0 0
  1. private static void copyFileUsingStream(File source, File dest) throws IOException {
  2.     InputStream is = null;
  3.     OutputStream os = null;
  4.     try {
  5.         is = new FileInputStream(source);
  6.         os = new FileOutputStream(dest);
  7.         byte[] buffer = new byte[1024];
  8.         int length;
  9.         while ((length = is.read(buffer)) > 0) {
  10.             os.write(buffer, 0, length);
  11.         }
  12.     } finally {
  13.         is.close();
  14.         os.close();
  15.     }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment