IO流(1)简单了解

1、IO流

操作文件

Input(读文件):把数据从物理内存加载到运行内存

Output(写文件):把数据从运行内存写出到物理内存

传输:本地传输、网络传输、对象传输

1.1 一个工具类 File

操作文件的类

1、文件的路径

正斜杠:左斜杠,/ ;Unix/Linux

反斜杠:右斜杠,\ ; Windows

在Java中,\代表转义

所以,在写路径的时候要写两个

image-20220808135025446

在File类中,定义了路径分隔符常量

image-20220808135426851

2、File类的构造器

image-20220808135632207

(1)最简单的

 File file = new File("E:\\ demo");

(2)其余两种

image-20220808140025228

2.1 操作文件

1、创建文件:creatFile()

异常??怕你没这个盘符

image-20220808140320018

  • 不会覆盖已有的文件

2、删除文件

没有异常?? boolean

不走回收站

image-20220808140442036

3.3 操作文件夹(目录)

1、创建文件夹:mkdir

File file = new File("E:/a");

boolean mkdir = file.mkdir(); // 创建一层文件夹

boolean mkdirs = file.mkdirs();  // 创建多级文件夹

mkdirs用的更多

2、删除

不走 回收站

与删文件一样

3、想直接一次性新建一个文件夹并且在文件夹里新建文件怎么办??

新建某个路径下的某一个文件,这个路径还不存在,(没有这个方法),我们可以封装一个工具类,怎么办????

嗯,,,自己没写出来

 public class FileUtil {

    /*
        分析:
        1.传入的路径是一个e:\\a\b\c\aaa.txt
        2.传入的路径是一个e:/a/b/c/bbb.txt
     */
    public static boolean createDirAndFile(String filepath) throws IOException {
        File file = null;
        if(filepath.contains("/")){
            if(filepath.contains("\\")){
                throw new RuntimeException("路径不合法");
            }else {
                // e:\\a\\b\\c\\a.txt
                int index = filepath.lastIndexOf("/");
                String filename = filepath.substring(index,filepath.length());
                filepath = filepath.substring(0, index+ 1);
                file = new File(filepath);
                file.mkdirs();
                // 创建文件
                File newFile = new File(filepath,filename);
                newFile.createNewFile();
                return true;
            }
        }
        if(filepath.contains("\\")){
            if(filepath.contains("/")){
                throw new RuntimeException("路径不合法");
            }else {
                // e:\\a\\b\\c\\a.txt
                int index = filepath.lastIndexOf("\\");
                String filename = filepath.substring(index,filepath.length());
                filepath = filepath.substring(0, index+ 1);
                file = new File(filepath);
                file.mkdirs();
                // 创建文件
                File newFile = new File(filepath,filename);
                newFile.createNewFile();
                return true;
            }
        }

        throw new RuntimeException("路径不合法");
    }

    public static void main(String[] args) {
//        String str = "e:\\a\\b\\c\\a.txt";
//        System.out.println(str.contains("/"));
        try {
            createDirAndFile("d");
            System.out.println("文件创建成功...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

4、判断文件是否存在 file.exists()

绝对路径:以盘符开头

相对路径:不以盘符开头,此时创建文件会创建到当前项目根路径下

5、判断是否是目录 file.isDirectory()

 判断是否是文件  `file.isFile()`

别的方法可看文档

6、获取文件绝对路径 file.getAbsolutePath(),返回值是String

​ 获取对应的相对路径的那个对象: file.getAbsoluteFile(),返回的是对象

file.getPath(),里面传什么就返回什么

7、文件夹列表的操作(重要)

image-20220808154338976

image-20220808153858600

image-20220808154207664

用过滤器不建议使用匿名表达式,建议单独写一个类

posted @ 2022-08-08 22:05  来日可追  阅读(58)  评论(1编辑  收藏  举报