Advertisement
Jakzon123

java IOStream copy file

Nov 13th, 2022 (edited)
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. // not an ideal implementation by any means, just an example method
  4.  
  5. public class FileIO {
  6.     public static void main(String[] args) throws Exception {
  7.         // create Files, in and out
  8.         File f1 = new File("in.txt");
  9.         File f2 = new File("out.txt");
  10.  
  11.         try {
  12.  
  13.             // create instream and outstream
  14.             FileInputStream in = new FileInputStream(f1);
  15.             FileOutputStream out = new FileOutputStream(f2);
  16.  
  17.             int i;
  18.             // while there is a char to be read, continue writing to outstream
  19.             while ((i = in.read()) != -1) {
  20.                 out.write(i);
  21.             }
  22.  
  23.             // close streams
  24.             in.close();
  25.             out.close();
  26.         }
  27.        
  28.         // catch any exceptions
  29.         catch (Exception e) {
  30.             System.exit(0);
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement