IO流

IO流

IO

I  input  输入

O  output  输出

IO流的分类

1、按照IO流的数据流动方向分  输入流  输出流

2、按照IO流的数据处理的最小单位分  字符流  字节流

3、根据IO流的作用分  节点流  处理流

IO流的四个抽象基类,超级父类

InputStream  字节输入流

OutputStream  字节输出流

Reader  字符输入流

Writer  字符输出流

和文件相关的IO流

类型

  FileInputStream

    文件字节输入流

    读取任意类型的文件

  FileOutputStream

    文件字节输出流

    写数据到任意类型的文件

  FileReader

    文件字符输入流

    只能读取纯文本文件(.java .txt .html .js .css ......)

  FileWriter

    文件字符输出流

    只能把数据保存到纯文本文件中

读取

  (1)读取一个纯文本文件

形式一:
//(1)指定要读取的文件
File file = new File("upload/exam.txt");
//(2)创建文本文件的输入流
FileReader fr = null;
try{
    fr = new FileReader(file);
    //(3)在当前程序中创建一个字符数组,用来保存每一次读取的文本信息
    char[] data = new char[10];
    //(4)用来记录每一次读取的字符个数
    int len;
    //(5)用来拼接从文件读取的信息
    StringBuilder sb = new StringBuilder();
    //(6)循环读取
    while((len = fr.read(data))!=-1){
        sb.append(new String(data,0,len));
    }
    System.out.println(sb);
}catch(Exception e){
    //....
}finally{
    //(7)释放资源
    try{
        if(fr!=null){
            fr.close();
        }
    }catch(Exception e){
    }
}
形式二:
File file = new File("upload/exam.txt");
try(
    FileReader fr = new FileReader(file);
){
    char[] data = new char[10];
    int len;
    StringBuilder sb = new StringBuilder();
    while((len = fr.read(data))!=-1){
        sb.append(new String(data,0,len));
    }
    System.out.println(sb);
}catch(Exception e){
    //....
}

  (2)读取任意类型的文件

//(1)指定文件
File file = new File(".....");
//(2)创建字节输入流
try(
    FileInputStream fis = new FileInputStream(file);
){
    //(3)创建字节数组,用来存储每次读取的内容
    byte[] data = new byte[1024];
    //(4)用len记录每次读取的字节数
    int len;
    
    //(5)循环读取
    while( (len = fis.read(data)) !=-1){
        //......
    }
    
}catch(Exception e){
    //...
}

保存

  (1)把数据保存到一个纯文本文件

//(1)指定要保存的文件
File file = new File("....");
try(
    FileWriter fw = new FileWriter(file);
){
    String info = "....."; //要写入到文件的数据内容
    fw.write(info);
    //
    char[] data = new char[1024];
    //...把数据保存到data中
    
    fw.write(data,0,len);
}catch(Exception e){
    //....
}

  (2)把数据保存到一个任意类型的文件

//(1)指定要保存的文件
File file = new File("....");
try(
    FileOutputSream fos  = new FileOutputSream(file);
){
    byte[] data = ....; //用来存储要输出到文件的内容
    fos.write(data,0 ,len);
}catch(Exception e){
    //....
}

复制

  一边读一边写

  纯文本文件

    public static void main(String[] args) {
        //(1)创建源文件对象
        File src = new File("1.txt");
        
        //(2)创建目标文件对象
        File dest = new File("2.txt");
        
        //(3)创建输入流
        FileReader fr = null;
        //(4)创建输出流
        FileWriter fw = null;
        
        try {
            fr = new FileReader(src);
//            fw = new FileWriter(dest);//覆盖模式
            fw = new FileWriter(dest,true);//追加模式
            
            //(5)一边读一边写
            //从fr读一些,就写入fw一些
            char[] data = new char[6];//1024
            while(true){
                int len = fr.read(data);
                if(len == -1){
                    break;
                }
                fw.write(data,0,len);//本次读了几个字符,就写几个字符
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                if(fr!=null){
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            try {
                if(fw!=null){
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

  任意类型文件

    public static void main(String[] args) {
        // (1)创建源文件对象
        File src = new File("2.jpeg");// 完整的描述:路径+文件名+后缀名
        // (2)创建目标文件对象
        File dest = new File("3.jpg");
        // (3)创建输入流
        // (4)创建输出流
        try (
                FileInputStream fis = new FileInputStream(src); 
                FileOutputStream fos = new FileOutputStream(dest);
            ) {
            byte[] data = new byte[10];
            int len;
            while ((len = fis.read(data)) != -1) {
                fos.write(data, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

File

  用来表示一个文件或者一个目录  实际上是一个抽象的路径名

  获取文件或目录的一些信息

    (1)获取文件的大小

      long length()

      如果要获取目录的大小,必须编写递归

    public long getDirLength(File dir){
        if(dir.isFile()){//如果是文件,直接返回文件的大小
            return dir.length();
        }else if(dir.isDirectory()){
            long sum = 0;
            File[] listFiles = dir.listFiles();
            for (File sub : listFiles) {
//                sum += 下一级的大小;
                sum += getDirLength(sub);
            }
            return sum;
        }
        return 0;//既不是文件又不是文件夹,不存在
    }

      如果文件不存在,返回0

    (2)获取文件或目录的名称

      String getName()

    (3)获取文件或目录的路径   

      String getPath():获取路径
      String getAbsolutePath():获取绝对路径
      String getCanonicalPath():获取规范路径,例如:../ /

    (4)获取文件的后缀名

      String name = file.getName();//得到文件名   包含扩展名

      String ext = name.substring(name.lastIndexOf('.'));

    (5)获取文件的最后修改时间

      long  lastModified()   毫秒数

      如果文件不存在,返回0

    (6)获取上一级目录

      String getParent()

      File getParentFile()

  判断

    (1)是否是文件

      isFile()  仅当file代表的文件存在,并且是个文件,才返回true

      如果文件不存在,返回false

    (2)是否是目录

      isDirectory()  仅当file对象代表的目录存在,并且是个文件夹目录,才返回true

      如果对应的不存在,返回false

    (3)是否存在

      exists()

    (4)是否隐藏

      isHidden()

    (5)文件是否可读

      canRead()

    (6)文件是否可写

      canWrite()

  操作

    (1)创建文件  

      createNewFile()

    (2)创建目录

      mkdir()  如果父目录不存在,那么创建失败

      mkdirs()  如果父目录不存在,也一并创建

    (3)删除文件或目录

      delete()

      只能删空目录

      如果要删除有内容的目录,需要使用递归

    public void delDir(File file){        
        //如果是目录
        if(file.isDirectory()){
            //(2)先获取下一级,并删除下一级
            //a:获取下一级
            File[] listFiles = file.listFiles();
            //b:遍历并删除下一级
            for (File sub : listFiles) {
                //这是一个重复的过程
                delDir(sub);//调用自己
            }
        }
        
        //删除自己
        file.delete();
    }

    (4)重命名

      renameTo(File newFile)

  操作文件夹

    获取它的下一级

      String[] list();

      File[] listFiles()

posted @ 2023-03-06 15:08  LuckySnail  阅读(26)  评论(0)    收藏  举报