1. 父路径+文件名
    • File(String parentPath,String child)
    • File(File parentPath,String child) //在File对象中传入File对象,便于底层操作
  2. 绝对路径
    • File(String path)
  3. 没有路径(默认当前项目)
    • File(String filename)
  4. 以当前项目构建
    • File(“./filename”)
  5. 以当前项目所在盘构建
    • File(/filename);
  • 代码实例:myproject->src->cn.haien.io.File->testPath

    public static void main(String[] args) {
        String parentPath="E:/xp/test";
        String name="2.jpg";
        //1.父路径+文件名
        File src=new File(parentPath,name); //文件路径+文件名
        src=new File(new File(parentPath),name);
        //输出
        System.out.println(src.getName()); //获取文件名  2.jpg
        System.out.println(src.getPath()); //获取文件路径  E:\xp\test\2.jpg
        //2.绝对路径
        src=new File("E:xp/test/2.jpg");
        System.out.println(src.getName()); // 2.jpg
        System.out.println(src.getPath()); // E:\xp\test\2.jpg
        //3.没有路径(当前项目)直接构建
        src=new File("test.txt");
        System.out.println(src.getName()); // test.txt
        System.out.println(src.getPath()); // test.txt,未传入路径构建则获取不到完整路径
        System.out.println(src.getAbsolutePath()); // D:\Eclipse\myprojects\myproject\test.txt
        //以当前项目下文件夹“.”下文件构建
        src=new File("./test.txt");
        System.out.println(src.getName()); // test.txt
        System.out.println(src.getPath()); // .\test.txt
        System.out.println(src.getAbsolutePath()); // D:\Eclipse\myprojects\myproject\.\test.txt
        //以项目所在盘构建
        src=new File("/test.txt");
        System.out.println(src.getName()); // test.txt
        System.out.println(src.getPath()); // \test.txt
        System.out.println(src.getAbsolutePath()); // D:\test.txt
    }
    
  • mkdir() 创建目录,若父目录不存在则创建失败
  • mkdirs() 创建目录,目录链不存在则一并创建

    path="E:/xp/test/parent/new";
    src=new File(path);
    //src.mkdir();
    src.mkdirs();
    
  • 查询目录下文件

    path="E://xp/test";
    src=new File(path);
    if(src.isDirectory()) {
        System.out.println("该目录下文件名:");
        String[] subNames=src.list(); //获得文件夹下各文件名
        for(String temp:subNames) {
            System.out.println(temp);
        }
        System.out.println("该目录下文件File对象:");
        File[] subFiles=src.listFiles();
        for(File temp:subFiles) {
            System.out.println(temp.getAbsolutePath());
        }
        System.out.println("打印子目录下java文件名:");
        //模板设计模式
        subFiles=src.listFiles(new FilenameFilter() {
    
            /**
            * 重写了文件过滤器,返回为真时才将文件挑选出来,否则将剔除出listFiles
            * dir 代表src
            */
            @Override
            public boolean accept(File dir, String name) {
                // TODO 自动生成的方法存根
                return new File(dir,name).isFile()&&name.endsWith(".java");
            }
        });
        for(File temp:subFiles) {
            System.out.println(temp.getAbsolutePath());
        }
    }
    
  • 递归输出子孙级目录(详述递归步骤)

    public static void printName(File src) {
        //所谓递归,就要先把第一层写出来--递归体
        if(null==src||!src.exists()) {
            return;
        }
        System.out.println(src.getAbsolutePath());
        //然后写递归头--递归条件
        if(src.isDirectory()) {
            for(File sub:src.listFiles()) {
                printName(sub); //里面就是自己调用自己
            }
        }
    }
    
  • file.renameto(file): 重命名,但这个几乎不会成功,因为它是不能跨平台的,它无法将一个文件在不同文件系统之间移动,而即使在同一台计算机上文件系统也可能不同,新的文件名也可能代表着不同的文件系统,即使文件类型相同,所以最好不要用这个方法,可以使用Files.move(srcPath,desPath).