sarath280

Untitled

Oct 1st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. Example for FileInputStream and FileOutputStream
  2.  
  3. import java.io.*;
  4. import java.nio.channels.*;
  5.  
  6.  
  7. public class CopyFileChannel {
  8.  
  9.     public static void main(String[] args) {
  10.         if (args.length < 2) {
  11.             System.out.println("Please provide input and output files");
  12.             System.exit(0);
  13.         }
  14.  
  15.         String inputFile = args[0];
  16.         String outputFile = args[1];
  17.  
  18.         try (
  19.             FileChannel sourceChannel = new FileInputStream(inputFile).getChannel();
  20.             FileChannel destChannel = new FileOutputStream(outputFile).getChannel();
  21.         ) {
  22.             sourceChannel.transferTo(0, sourceChannel.size(), destChannel);
  23.         } catch (IOException ex) {
  24.             ex.printStackTrace();
  25.         }
  26.     }
  27. }
Add Comment
Please, Sign In to add comment