1. import java.io.BufferedInputStream; 
  2. import java.io.File; 
  3. import java.io.FileInputStream; 
  4. import java.io.FileOutputStream; 
  5. import java.io.IOException; 
  6. import java.nio.ByteBuffer; 
  7. import java.nio.channels.FileChannel; 
  8.  
  9. public class FileCopy{ 
  10.     String fromFile="c:/a.txt"
  11.     String toFile="d:/b.txt"
  12.      
  13.     public static void main(String[] args) { 
  14.          
  15.          FileCopy fileCopy = new FileCopy(); 
  16. //       fileCopy.nioFileCopy(); 
  17.          fileCopy.streamFileCopyWithBuffer(); 
  18. //       fileCopy.streamFileCopy(); 
  19.     } 
  20.      
  21.     public void streamFileCopyWithBuffer(){ 
  22.         try { 
  23.  
  24.             File fileIn = new File(fromFile); 
  25.             File fileOut = new File(toFile); 
  26.             FileInputStream fin = new FileInputStream(fileIn); 
  27.             BufferedInputStream bin = new BufferedInputStream(fin); 
  28.             FileOutputStream fout = new FileOutputStream(fileOut); 
  29.             byte[] buffer = new byte[8192]; 
  30.             while (bin.read(buffer) != -1) { 
  31.                 fout.write(buffer); 
  32.             } 
  33.         } catch (IOException e) { 
  34.             e.printStackTrace(); 
  35.         } 
  36.     } 
  37.    
  38.    public void streamFileCopy(){ 
  39.        try { 
  40.  
  41.            File fileIn = new File(fromFile); 
  42.            File fileOut = new File(toFile); 
  43.            FileInputStream fin = new FileInputStream(fileIn); 
  44.            FileOutputStream fout = new FileOutputStream(fileOut); 
  45.            byte[] buffer = new byte[8192]; 
  46.            while (fin.read(buffer) != -1) { 
  47.                fout.write(buffer); 
  48.            } 
  49.        } catch (IOException e) { 
  50.            e.printStackTrace(); 
  51.        } 
  52.    } 
  53.     public void nioFileCopy() { 
  54.         try { 
  55.  
  56.             File fileIn = new File(fromFile); 
  57.             File fileOut = new File(toFile); 
  58.             FileInputStream fin = new FileInputStream(fileIn); 
  59.             FileOutputStream fout = new FileOutputStream(fileOut); 
  60.  
  61.             FileChannel fcIn = fin.getChannel(); 
  62.             ByteBuffer bf = ByteBuffer.allocate(8192); 
  63.             FileChannel fcOut = fout.getChannel(); 
  64.             while (fcIn.read(bf) != -1) { 
  65.                 bf.flip(); 
  66.                 fcOut.write(bf); 
  67.                 bf.clear(); 
  68.             } 
  69.  
  70.         } catch (IOException e) { 
  71.             e.printStackTrace(); 
  72.         } 
  73.         
  74.  
  75.     } 
  76.  
  77.