1 package cn.itsource._inputsteam; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 /** 9 * 该类是用于文件复制 10 * @author Administrator 11 * 12 */ 13 public class FileCopy { 14 15 public static void main(String[] args) { 16 // 复制一个文件到指定目录下面 17 FileInputStream fis = null; 18 FileOutputStream fos = null; 19 try{ 20 fis = new FileInputStream("E:/好看的.txt"); 21 fos = new FileOutputStream("E:/好看的-copy.txt"); 22 byte[] b = new byte[1024]; 23 int read = -1; //表示每一次真正读了多少字节到byte数组 24 while ((read = fis.read(b)) != -1){ //最常用 25 // 读一次写一次 26 fos.write(b, 0, read);// read表示每次读取的字节数 27 28 } 29 } catch (FileNotFoundException e) { 30 e.printStackTrace(); 31 } catch (IOException e) { 32 e.printStackTrace(); 33 } finally { 34 //关闭流 35 try { 36 if(fos != null){ 37 fis.close(); 38 } 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } finally { 42 try { 43 //关闭流原则:先开后关 44 if(fis != null){ 45 fis.close(); 46 } 47 48 } catch (IOException e) { 49 e.printStackTrace(); 50 } 51 } 52 } 53 } 54 55 56 }
标签:11,fis,java,22,fos,read,IOException,2022,import From: https://www.cnblogs.com/puwei520/p/16916451.html