JavaIO copy file example and javaio copy example

Source: Internet
Author: User

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}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.