From bde81332a69bcae5a4e998031b518638daaf428b Mon Sep 17 00:00:00 2001 From: Muhammad Waleed Ahmad <115546135+MuhammadWaleed-0786@users.noreply.github.com> Date: Sun, 15 Oct 2023 01:15:54 +0500 Subject: [PATCH] Create FileCopy.java Create FileCopy.java --- FileCopy.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 FileCopy.java diff --git a/FileCopy.java b/FileCopy.java new file mode 100644 index 00000000..aa0c2576 --- /dev/null +++ b/FileCopy.java @@ -0,0 +1,20 @@ +import java.io.*; + +public class FileCopy { + public static void main(String[] args) { + try (InputStream inputStream = new FileInputStream("source.txt"); + OutputStream outputStream = new FileOutputStream("destination.txt")) { + + byte[] buffer = new byte[1024]; + int bytesRead; + + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + + System.out.println("File copied successfully."); + } catch (IOException e) { + e.printStackTrace(); + } + } +}