Advertisement
joseleonweb

Untitled

Feb 27th, 2021
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class CopyFile {
  4.     public static void main(String[] args) throws IOException
  5.     {
  6.         int i;
  7.         FileInputStream fin = null;
  8.         FileOutputStream fout = null;
  9.        
  10.         // First, make sure that both files has been specified.
  11.         if(args.length != 2) {
  12.             System.out.println("Usage: Copy File from to");
  13.             return;
  14.         }
  15.        
  16.         // Copy a File
  17.         try {
  18.             // Attempt to open the files
  19.             fin = new FileInputStream(args[0]);
  20.             fout = new FileOutputStream(args[1]);
  21.            
  22.             do {
  23.                 i = fin.read();            // Read bytes from one file
  24.                 if(i != -1) fout.write(i); // and write them to another
  25.             } while(i != -1);
  26.        
  27.         } catch(IOException exc) {
  28.             System.out.println("I/O Error: " + exc);
  29.         } finally {
  30.             try {
  31.                 if(fin != null) fin.close();
  32.             } catch(IOException exc) {
  33.                 System.out.println("Error Closing Input File");
  34.             }
  35.             try {
  36.                 if(fout != null) fin.close();
  37.             } catch(IOException exc) {
  38.                 System.out.println("Error Closing Output File");
  39.             }
  40.         }
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement