• 【第十三章 节点流-字节型(FileInputStream,FileOutputStream)的使用,图片文件复制,指定路径下文件的复制】


    第十三章 节点流-字节型(FileInputStream,FileOutputStream)的使用,图片文件复制,指定路径下文件的复制

    1. FileInputStream读入数据
    1.对于文本文件(.txt,.java,.c,.cpp),使用字符流来处理;
    2.对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt),使用字节流来处理。

     /**
         * 1.使用字节流FileInputStream处理文本文件可能出现乱码
         */
        @Test
        public void testFileInputStream() {
            FileInputStream fileInputStream= null;
            try {
                //1.造文件
                File file=new File("hello.txt");
                //2.造流
                fileInputStream = new FileInputStream(file);
                //3.读数据
                byte[] buffer=new byte[5];
                int len;//记录每次读取的字节个数
                while((len=fileInputStream.read(buffer))!=-1){
                    String str=new String(buffer,0,len);
                    System.out.print(str);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    //4.关闭资源
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    2.使用FileInputStream和FileOutputStream实现图片文件复制

    /**
         * 2.实现对图片的复制操作
         */
        @Test
        public void testFileInputOutputStream() {
            FileInputStream fileInputStream= null;
            FileOutputStream fileOutputStream= null;
            try {
                File fileinput=new File("lay1.jpg");
                File fileoutput=new File("lay2.jpg");
                fileInputStream = new FileInputStream(fileinput);
                fileOutputStream = new FileOutputStream(fileoutput);
                //复制的过程
                byte[] buffer=new byte[5];
                int len;
                while ((len=fileInputStream.read(buffer))!=-1){
                    fileOutputStream.write(buffer,0,len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fileInputStream!=null){
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fileOutputStream!=null){
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    3.指定路径下文件的复制

      /**
        指定路径下文件的复制
         */
        public  void  copyfile(String srcPath,String destPath){//(原Path,复制到的Path)
            FileInputStream fileInputStream= null;
            FileOutputStream fileOutputStream= null;
            try {
                File srcFile=new File(srcPath);
                File destFile=new File(destPath);
                fileInputStream = new FileInputStream(srcFile);
                fileOutputStream = new FileOutputStream(destFile);
                //复制的过程
                byte[] buffer=new byte[1024];
                int len;
                while ((len=fileInputStream.read(buffer))!=-1){
                    fileOutputStream.write(buffer,0,len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fileInputStream!=null){
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fileOutputStream!=null){
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        @Test
        public  void testCopyFile(){
            long start=System.currentTimeMillis();
            String srcPath="lay1.jpg";
            String destPath="lay2.jpg";
            copyfile(srcPath,destPath);
            long end=System.currentTimeMillis();
            System.out.println("复制花费时间为:"+(end-start));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
  • 相关阅读:
    懒人的百宝箱「GitHub 热点速览」
    【IOS-初学】利用分段选择器和滑动开关(条)等等实现简单的图片浏览器-透明度和图片切换功能
    TensorFlow.NET--数据类型与张量详解
    JDK更换版本配置不生效问题
    私域流量的优势
    IDEA的使用(一) (IntelliJ IDEA 2022.1.3版本)
    在VScode中使用sftp传输本地文件到服务器端
    nginx如何安装 以及nginx的配置文件
    Go的安装及环境变量的配置
    【Taro3踩坑日记】不存在全局配置文件:C:\Users\TYW\.taro-global-config\index.json
  • 原文地址:https://blog.csdn.net/qq_43742813/article/details/127605781