File类

2019-06-22  本文已影响0人  kanaSki

File可以表示文件或者文件夹(不一定真实存在)

static String pathSeparator 与系统相关的路径分隔符,表示为字符串
static char pathSeparatorChar 与系统相关的路径分隔符
static String separator 与系统相关的默认名称分隔符,表示为字符串
static char separatorChar 与系统相关的默认名称分隔符

写路径的两种方式:
1、使用/:F:/Program/MyFirstWeb/MyPro01/test.txt (推荐)
2、常量拼接:path="F:'+File.separator+"Program"+....

        // \名称分割符
        String path = "F:\\SpringBootProgram\\MyFirstWeb\\MyPro01\\test.txt";
        File file = new File(path);
        // 输出文件长度(字节),文件夹的长度将为0
        System.out.println(file.length());
        File file1 = new File("F:\\SpringBootProgram\\MyFirstWeb\\MyPro01", "text.txt");

        /**
         * 相对路径与绝对路径
         * 绝对路径:windows中存在盘符
         * 相对路径:相对于当前项目(System.getProperty("user.dir"))
         */
        file = new File("test.txt");
        System.out.println(file.getAbsolutePath()); // 输出绝对路径
image.png
上一篇 下一篇

猜你喜欢

热点阅读