JavaIO copy file example and javaio copy example
1 package cn.com. demo; 2 3 import java. io. bufferedInputStream; 4 import java. io. bufferedOutputStream; 5 import java. io. fileInputStream; 6 import java. io. fileOutputStream; 7 import java. io. IOException; 8 import java. io. inputStream; 9 import java. io. outputStream; 10 11 public class StreamDemo {12 13 public static void main (String [] args) {14 StreamDemo streamDemo = new StreamDemo (); 15 try {16 String s RcFile = "E :\\ \ javaEE 5.0.chm"; 17 String tarFile = "E: \ \ javaEE 5.0 (2 ). chm "; 18 streamDemo. copyFile (srcFile, tarFile); 19} catch (Exception e) {20 e. printStackTrace (); 21} 22 23} 24/*** copy the file, use BufferedInputStream and BufferedOutputStream26 * @ param srcFile27 * @ param tarFile28 * @ throws IOException29 */30 public void copyFile (String srcFile, String tarFile) throws IOException {31 InputStr Eam input = null; 32 BufferedInputStream buffInput = null; 33 OutputStream output = null; 34 BufferedOutputStream buffOutput = null; 35 try {36 byte [] buffer = new byte [1024]; 37 int length = 0; 38 input = new FileInputStream (srcFile); 39 buffInput = new BufferedInputStream (input); 40 output = new FileOutputStream (tarFile ); 41 buffOutput = new BufferedOutputStream (output); 42 while (length = buffInput. read (B Uffer)> 0) {43 buffOutput. write (buffer, 0, length); 44} 45 buffOutput. flush (); 46} catch (Exception e) {47 throw new RuntimeException (e. getMessage (); 48} finally {49 if (input! = Null) {50 input. close (); 51 input = null; 52} 53 if (buffInput! = Null) {54 buffInput. close (); 55 buffInput = null; 56} 57 if (output! = Null) {58 output. flush (); 59 output. close (); 60 output = null; 61} 62 if (buffOutput! = Null) {63 buffOutput. close (); 64 buffOutput = null; 65} 66} 67 68} 69}