Io流和File

Io流

  1. java.io流四大家族(都是抽象类)

    • java.io.InputStream 字节输入流

    • java.io.OutputStream 字节输入流

    • java.io.Reader 字节输入流

    • java.io.Writer 字节输入流

      注意:在java中主要类目 以Stream结尾的都是字节流,以Readr/、writer结尾的都是字符流

    结论:所有的流都实现了close();方法,所有流使用了之后都必须关闭,所有的输出流都是可刷新的都有flush()方法,养成一个好习惯,输出流在最终输出之后,一定要要记得flush()刷新一下,这个刷新表示将通道当中剩余的未输出的数据强行输出(清空管道)

    1. 文件专属

      • Java。io.FileInputStream

      • Java。io.FileOutputStream

      • Java。io.FileReader

      • Java。io.FileWriter

    2. 转换流(将字节流转换成字符流)

      • Java。io.InputStreamReader

      • Java。io.OutputStreamWriter

    3. 缓冲流专属

      • Java。io.BUfferedInputStream

      • Java。io.BUfferedOutputStream

      • Java。io.BUfferedReader

      • Java。io.BUfferedWriter

    4. 数据流专属

      • Java。io.AtaInputStream

      • Java。io.DataOutputStream

    5. 标准输出流

      • Java。io.PrintWriter

      • Java。io.PrintStream

    6. 对象专属流

      • Java。io.ObjectOutputStream

      • Java。io.ObjectInputStream

  2. public static void main(String[] args) {
        FileInputStream fi = null;
        try {
            fi = new FileInputStream("D:/temp.txt/");
            //创建一个数组,通过数组来一次性夺取多个字节
             byte[] bytes = new byte[4];
            int readCount = fi.read(bytes);//输出的并不是字节本身,而是字节数量
            System.out.println(readCount);  
            //采用String的方法转换成字符串
            System.out.println(new String(bytes , 0 , readCount)); //通过这种方式取出每次输出的字节
            //采用while可以更加快捷
             int readCount = 0;
                while((readCount = fi.read(bytes)) !=-1){
                  System.out.println(new String(bytes , 0 , readCount)); //通过这种方式取出每次输出的字节
                }
                  
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
             if (fi !=null){
                try {
                    fi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

  1. 还可以采用另外一种方法,但是这种方式不能适合特别大的文件

    public static void main(String[] args) {
        FileInputStream fi =  null;
        try {
            //创建获取文件路径,并且捕捉异常
            fi = new FileInputStream("D:/temp.txt/");
            //获取还没有读取的字节数量
            int av = fi.available();
            System.out.println(av);
            //创立一个数组,让数组的长度等于字节数量,通过avaiable来获取字节数量
            byte[] b = new byte[fi.available()];//此时我们创建的数组的长度与字节数量相同,可以一次性读取完
             //读取字节
            int readCount = fi.read(b);
            System.out.println(new String(b)); //用String 的方法转换成字符串
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fi != null){
                try {
                    fi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

  1. Outputstream(写)

    • 这一种是清空文件所有东西然后在文件开始写入将字节转换成字母

       public static void main(String[] args) {
            FileOutputStream fi = null;
            try {
                //写
                fi = new FileOutputStream("mylove");
                //创建一个数组
                byte[] b = {97,98,99,100};
                 fi.write(b); //这一种是清空文件所有东西然后在文件开始写入将字节转换成字母
               
    ​
                fi.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (fi != null){
                    try {
                        fi.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    • 这一种是不清空文件所有东西然后在文件末尾写入将字节转换成字母

      public static void main(String[] args) {
          FileOutputStream fi = null;
          try {
              //写
              fi = new FileOutputStream("mylove" ,true);
              //创建一个数组
              byte[] b = {97,98,99,100};
               fi.write(b); //这一种是清空文件所有东西然后在文件开始写入将字节转换成字母
                 String s = "我是一个中国人,我骄傲!";//
              byte[] bytes = s.getBytes();  //将s转换到数组里面
                   fi.write(bytes); //在文件的末尾加入刚转换的东西
              fi.flush();//清空通道
          } catch (IOException e) {
              e.printStackTrace();
          }finally {
              if (fi != null){
                  try {
                      fi.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }

  1. FileReader

 public static void main(String[] args) {
         FileReader reader = null;
        try {
            reader = new FileReader("D:/temp.txt/");
            //创建一个char数组
              char[] c= new char[4];
            int readCount = 0;
            while((readCount=reader.read(c))!=-1){
                System.out.print(new String(c , 0,readCount));
            }
      
​
​
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

  1. FileReader

  2.     FileWriter wr = null;
        try {
            wr =new FileWriter("China");
             //创建一个数组
            char[] chars = {'我','是','中','国','人'};
            //开始写
            wr.write(chars);
            //继续写后面三个字
            wr.write(chars , 2 , 3);//每次的书写都是清空了源文件之后才重新写的
            
            wr.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(wr!=null){
                try {
                    wr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

  1. 复制一个文件

       FileReader in = null;
            FileWriter wr =null;
            try {
                //将in位置的文件负责到wr位置
                in = new FileReader("China");
                wr = new FileWriter("China.java");
           // 创建一个数组
                 char[] chars = new char[1024*520];
                 int readCount =0;
                 //一边读 一边写
                 while((readCount = in.read(chars))!=-1){
                      wr.write( chars, 0 ,readCount);
                 }
                wr.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (in != null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (wr !=null){
                        try {
                            wr.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
    ​
        }
    }

BufferedReader:

  • 带有缓冲区的字符输入流

  • 使用这个流的时候不需要自定义char数组,或者说不需要自定义byte数组,自带缓冲区

  1. 当一个流的构造方法中需要一个流的时候,被传进来的流叫做:节点流

  2. 外部负责包装的这个流叫做:包装流,还有一个名字叫处理流

public static void main(String[] args) {
    try {
        FileReader c =new FileReader("China");//输入流
        BufferedReader bf = new BufferedReader(c);//处理流
      /*  String firstline = bf.readLine();
        System.out.println(firstline);//读取第一行
        String Secondline = bf.readLine();//读取第二行
        System.out.println(Secondline);
        String Thirdline = bf.readLine();//读取第二行
        System.out.println(Thirdline);*/
        //采用while循环
        String line =null;
         while((line = bf.readLine())!=null){
                    System.out.println(line);   
         }
        bf.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

  1. BufferedWrite

    public static void main(String[] args) {
        BufferedWriter wr = null;
        try {
             wr = new BufferedWriter(new FileWriter("China.java", true));
            wr.write("德玛西亚");
            wr.write("洛克萨斯");
            wr.write("虽远必诛");
    ​
            wr.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (wr !=null){
                try {
                    wr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    ​
    }

File类

  1. File类和四大家族没有关系,所以File类不能完成文件的都读和写

  2. File对象代表什么?

    • 文件和目录路径名的抽象表现形式

    • C:\Drivers 这是一个file对象

    • 一个file对象可能是对应的目录,有可能是文件

     public static void main(String[] args) {
              //创建对象
            File file = new File("D:/ddd");
             //判断是否存在
            System.out.println(file.exists());//false
            //如果ddd不存在就创建一个(创建的文件)用file.mkdirs();创建文件夹
            if (!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            File file1 =new File("D:/ccc");
            System.out.println(file1.exists());//true
            File file2 =new File("D:/c/d/f/e/r");
             //创建一个文件夹c包含d包含你f包含e包含r
            if (!file2.exists()){
                file2.mkdirs();
            }
            //获取绝对路径
            File file3 = new File("China.java");
            System.out.println(file3.getAbsoluteFile());
    ​
        }

  1. 作业!

    将D:、course拷贝到C盘跟下

    需要使用、fileInputStream

    FileOutputStream

    File 和递归

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值