首页 > 代码库 > java IO流 ---文件的读写操作
java IO流 ---文件的读写操作
1 package com.io; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 public class Test2 { 8 9 /** 10 * @param args 11 * @throws IOException 12 */ 13 public static void main(String[] args) { 14 15 File file1 = new File("C:\\Users\\Coda\\Desktop\\google1.jpg"); 16 File file2 = new File("C:\\Users\\Coda\\Desktop\\图片储存\\dd.jpg"); 17 18 CopyIt(file1,file2); 19 20 } 21 22 public static void CopyIt( File file1, File file2) { 23 24 FileInputStream fileInputStream; 25 try { 26 fileInputStream = new FileInputStream(file1); 27 } catch (FileNotFoundException e) { 28 System.out.println("文件路径错误"); 29 throw new RuntimeException(e); 30 } 31 FileOutputStream fileOutputStream = null; 32 try { 33 fileOutputStream = new FileOutputStream(file2,true); 34 } catch (FileNotFoundException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 39 byte[]b = new byte[2]; 40 int count = 0; 41 try { 42 while((count = fileInputStream.read(b))!=-1){ 43 44 fileOutputStream.write(b); 45 46 } 47 } catch (IOException e) { 48 System.out.println("写入文件失败"); 49 throw new RuntimeException(e); 50 51 } finally{ 52 try { 53 fileOutputStream.close(); 54 } catch (IOException e) { 55 System.out.println("文件正在运行,无法关闭"); 56 throw new RuntimeException(e); 57 } 58 try { 59 fileInputStream.close(); 60 } catch (IOException e) { 61 System.out.println("文件正在运行,无法关闭"); 62 throw new RuntimeException(e); 63 } 64 System.out.println("文件复制成功"); 65 } 66 67 } 68 69 }
java IO流 ---文件的读写操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。