Java 测试: 图片复制

|--需求说明

 

|--实现思路

见代码注释

|--代码内容

 1 import java.io.FileInputStream;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 
 6 /**
 7  * @auther::9527
 8  * @Description: 第十题
 9  * @program: 多线程
10  * @create: 2019-08-10 00:26
11  */
12 public class Tenth {
13     public static void main(String[] args) {
14         FileInputStream fis = null;
15         FileOutputStream fos = null;
16 
17         try {
18             //确定输入输出的文件名
19             fis = new FileInputStream("c:/a.jpg");
20             fos = new FileOutputStream("c:/b.jpg");
21             //设定判定器,判定是否读取完毕
22             int temp = 0;
23             //开始读取数据,如果没有读完就继续读,按read()方法所说,读完会成为-1,若取值不为-1,则持续读取
24             while ((temp = fis.read())!=-1){
25                 //将读取到的信息写入文件
26                 fos.write(temp);
27             }
28             System.out.println("已将c盘的a.jpg复制为b.jpg");
29         } catch (FileNotFoundException e) {
30             e.printStackTrace();
31         } catch (IOException e) {
32             e.printStackTrace();
33         }
34         try {
35             //关闭输出流和输入流
36             fos.close();
37             fis.close();
38         } catch (IOException e) {
39             e.printStackTrace();
40         }
41     }
42 }
使用IO复制图片

|--运行结果

猜你喜欢

转载自www.cnblogs.com/twuxian/p/11330260.html