java进阶(33)--IO流 (3)

 

java进阶(33)--IO流

 (2)查看输出结果,追加一段String字符串

java进阶(33)--IO流

4、文件复制(FileInputStream+FileOutputStream)

(1)流程:拷贝过程是一边读、一边写;文件类型任意,是万能的

(2)举例文件复制:

1 package JAVAADVANCE; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 public class TestAdvance33IOTest08 { 9 public static void main(String[] args) { 10 FileInputStream fis=null; 11 FileOutputStream fos=null; 12 try { 13 //创建一个输入对象流 14 fis=new FileInputStream("D:\\javaTest\\inFile\\甜蜜家园第01集.mp4"); 15 //创建一个输出对象流 16 fos=new FileOutputStream("D:\\javaTest\\outFile\\甜蜜家园第01集.mp4"); 17 //准备一个byte数组,1024byte=1KB,*1024=1M,一次最多读1M 18 byte[] bytes=new byte[1024*1024]; 19 int readCount=0; 20 while ((readCount=fis.read(bytes))!=-1) 21 { 22 //读取多少,写多少 23 fos.write(bytes,0,readCount); 24 } 25 fos.flush(); 26 } catch (FileNotFoundException e) { 27 e.printStackTrace(); 28 } catch (IOException e) { 29 e.printStackTrace(); 30 } finally { 31 //fos与fis的关闭分开try catch比较好,避免互相影响,有流没有关闭 32 if (fos != null) { 33 try { 34 fos.close(); 35 } catch (IOException e) { 36 e.printStackTrace(); 37 } 38 } 39 if(fis!=null){ 40 try { 41 fis.close(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 } 47 48 49 50 51 } 52 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppfsd.html