java IO流 复制图片

(一)使用字节流复制图片

 1 //字节流方法
 2     public static void copyFile()throws IOException {
 3         
 4         //1.获取目标路径
 5         //(1)可以通过字符串
 6 //        String srcPath = "C:\Users\bg\Desktop\截图笔记\11.jpg";
 7 //        String destPath = "C:\Users\bg\Desktop\图片备份\11.jpg";
 8         
 9         //(2)通过文件类
10         File srcPath = new File("C:\Users\bg\Desktop\截图笔记\22.PNG");
11         File destPath = new File("C:\Users\bg\Desktop\图片备份\22.PNG");
12         
13         //2.创建通道,依次 打开输入流,输出流
14         FileInputStream fis = new FileInputStream(srcPath);
15         FileOutputStream fos = new FileOutputStream(destPath);
16 
17         byte[] bt = new byte[1024];
18         
19         //3.读取和写入信息(边读取边写入)
20         while (fis.read(bt) != -1) {//读取
21             fos.write(bt);//写入
22         }
23 
24         //4.依次 关闭流(先开后关,后开先关)
25         fos.close();
26         fis.close();
27     }

(二)使用字符流复制文件

 1 //字符流方法,写入的数据会有丢失
 2     public static void copyFileChar()throws IOException {
 3         
 4         //获取目标路径
 5         File srcPath = new File("C:\Users\bg\Desktop\截图笔记\33.PNG");
 6         File destPath = new File("C:\Users\bg\Desktop\图片备份\33.PNG");
 7         
 8         //创建通道,依次 打开输入流,输出流
 9         FileReader frd = new FileReader(srcPath);
10         FileWriter fwt = new FileWriter(destPath);
11 
12         char[] ch = new char[1024];
13         int length = 0;
14         // 读取和写入信息(边读取边写入)
15         while ((length = frd.read(ch)) != -1) {//读取
16             fwt.write(ch,0,length);//写入
17             fwt.flush();
18         }
19 
20         // 依次 关闭流(先开后关,后开先关)
21         frd.close();
22         fwt.close();
23     }

(三)以复制图片为例,实现抛出异常案例

 1     //以复制图片为例,实现try{ }cater{ }finally{ } 的使用
 2         public static void test(){
 3             //1.获取目标路径
 4             File srcPath = new File("C:\Users\bg\Desktop\截图笔记\55.PNG");
 5             File destPath = new File("C:\Users\bg\Desktop\图片备份\55.PNG");
 6             //2.创建通道,先赋空值
 7             FileInputStream fis = null;
 8             FileOutputStream fos = null;
 9             //3.创建通道时需要抛出异常
10             try {
11                 fis = new FileInputStream(srcPath);
12                 fos = new FileOutputStream(destPath);
13                 
14                 byte[] bt = new byte[1024];
15                 //4.读取和写入数据时需要抛出异常
16                 try {
17                     while(fis.read(bt) != -1){
18                         fos.write(bt);
19                     }
20                 } catch (Exception e) {
21                     System.out.println("储存盘异常,请修理");
22                     throw new RuntimeException(e);
23                 }
24                 
25                 
26             } catch (FileNotFoundException e) {
27                 System.out.println("资源文件不存在");
28                 throw new RuntimeException(e); 
29             }finally{
30                 
31                 //5.无论有无异常,需要关闭资源(分别抛出异常)
32                 try {
33                     fos.close();
34                 } catch (Exception e) {
35                     System.out.println("资源文件或目标文件关闭失败!");
36                     throw new RuntimeException(e);
37                 }
38                 
39                 try {
40                     fis.close();
41                 } catch (IOException e) {
42                     System.out.println("资源文件或目标文件关闭失败!");
43                     throw new RuntimeException(e);
44                 }
45                 
46             }
47         }

字符流  =  字节流 + 解码 --->找对应的码表  GBK

  字符流解码 拿到系统默认的编码方式来解码

将图片中的二进制数据和GBK码表中的值进行对比, 对比的时候会出现二进制文件在码表中找不对应的值,他会将二进制数据标记为未知字符,当我在写入数据的是后会将未知的字符丢掉。所以会造成图片拷贝不成功(丢失数据)

疑问:何时使用字节流?何时使用字符流?

  使用字节流的场景:读写的数据不需要转为我能够看得懂的字符。比如:图片,视频,音频...

  使用字符流的场景 :如果读写的是字符数据。

原文地址:https://www.cnblogs.com/bigerf/p/6144840.html