Java--IO--FileInputStream和FileOutputStream

  1. FileInputStream的:文件字节输入流(InputStream的子类)
    1. FileInputStream的方法:
      1.  

         

    2. FileInputStream的实例:

      1. package com.model.io.inputstream.fileinputstream;
        
        import org.junit.Test;
        
        import java.beans.Transient;
        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        import java.io.IOException;
        
        /**
         * @Description:测试类
         * @Author: 张紫韩
         * @Crete 2021/6/18 11:28
         */
        public class FileInputStreamDemo01 {
        
            /**演示:文件字节输入流:FileInputStream
             *
             * 单个字节读效率比较低,且读取文本文件会发生乱码问题,
             * (一个汉字三个字节 ,一次只读取一个字节,只读取了汉字的三分之一就输出所以会发生乱码错误)
             * */
        
            @Test
            public void read01() throws IOException {
                //1.创建文件对象
                File file = new File("D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\aa.txt");
                //2.创建读取文件的对象
                FileInputStream fileInputStream=null;
                //3.声明接受读取信息的变量
                int readDate=0;
                try {
                    //4.向文件字节输入流对象传入读取的文件
                   fileInputStream=new FileInputStream(file);
                   //5.将文件读取对象的值赋给文件信息接受变量,当读取的为 -1 时说明文件内容已经全部读取,否则还需要继续读取
                   while((readDate=fileInputStream.read())!=-1){
                       System.out.print((char) readDate);//将读取的字节信息转换为字符,输出到控制台
                   }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }finally {
                    //6.关闭字节读取对象
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        
            @Test
            public void read02() throws IOException {
        
                //1.创建要读取的文件
                File file = new File("D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\aa.txt");
                //2.创建一个一次读取后返回的读取个数
                int readCount=0;
                //3.声明一次读取后,装载数据的容器
                byte[] buff=new byte[8];
                //4.声明读取数据的对象
                FileInputStream fileInputStream=null;
                try {
                    //5.将需要读取文件传入到字节输入流对象中
                    fileInputStream = new FileInputStream(file);
                    //6.每次读取都会读取8个字节的数据,且放在buff中,readCount是真实读取的个数
                    while ((readCount=fileInputStream.read(buff))!=-1){
                        //7.将读取的buff字节数组转换成字符串输出
                        System.out.println(new String(buff, 0, readCount));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    //8.关闭字节输入流对象
                    fileInputStream.close();
                }
        
            }
        
        }
  2. FileOutputStream:文件字节输出流(是OutputStream的子类)

    1. 常见的方法:  

    2. FileOutputStream的实例:
      1. package com.model.io.outputstream.fileoutputstream;
        
        import org.junit.Test;
        
        import java.io.FileNotFoundException;
        import java.io.FileOutputStream;
        import java.io.IOException;
        
        /**
         * @Description:测试类
         * @Author: 张紫韩
         * @Crete 2021/6/18 13:55
         */
        public class FileOutPutStreamDemo01 {
            /**
             * 演示FileOutputStream,将数据写入到磁盘中的某个文件
             * 如果磁盘中的这个文件不存在,则创建文件
             * */
            @Test
            public void write() throws IOException {
                //1.声明写入文件的路径
                String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\OutputStream\\FileOutputStream.txt";
                //2.声明写入字节对象
                FileOutputStream fileOutputStream=null;
                try {
                    //3.将文件的路径传入给写入字节对象
        //           fileOutputStream = new FileOutputStream(filePath); //第一种是替换的
                    fileOutputStream=new FileOutputStream(filePath,true); //追加的方式添加到文件中
        
                    //4.写入字节对象调用write,向文件中写入数据
        //          fileOutputStream.write('a');  //第一种只写入一个字符
                    String str="hello,word!";
        
        //          fileOutputStream.write(str.getBytes());  //第二中写入的是一个字节数组,将字符串转为字节数组写入到文件中
                    fileOutputStream.write(str.getBytes(), 0, str.length()); //写入的字节数组指定起始位置和长度
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }finally {
                    //5.关闭资源
                    fileOutputStream.close();
                }
        
        
            }
        }
  3.  

    完成文件的拷贝
    1. package com.model.io;
      
      import java.io.*;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/6/18 14:50
       */
      public class CopyDemo01 {
          /**
           * 完成文件的拷贝,将D:\qq\IDEA\IdeaProjects\java_mianshi_test\mianshi_io\src\main\resources\a.jpg
           * 文件拷贝到D:\qq\IDEA\IdeaProjects\java_mianshi_test\mianshi_io\src\main\resources\File 目录下
           *
           * */
          public static void main(String[] args) throws IOException {
              File fileFrom = new File("D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\a.jpg");
      
              String fileTo="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\File\\"+fileFrom.getName();
      
               //1.先读取到文件
              FileInputStream fileInputStream=null;
              byte[] buff = new byte[1024];
              int readCount=0;
              FileOutputStream fileOutputStream=null;
              try {
                  fileInputStream=new FileInputStream(fileFrom);
                  fileOutputStream=new FileOutputStream(fileTo,true);
                  while((readCount=fileInputStream.read(buff))!=-1){
                      //2.再将文件写入到指定的目录下
                      fileOutputStream.write(buff,0,readCount);
                  }
                  System.out.println(fileFrom.getName()+"拷贝成功!");
      
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              }finally {
                  if (fileOutputStream!=null) {
                      fileOutputStream.close();
                  }
                  if (fileOutputStream!=null) {
                      fileInputStream.close();
                  }
              }
      
          }
      }

       

         
posted @ 2021-06-18 15:08  张紫韩  阅读(108)  评论(0编辑  收藏  举报