file的常用方法

...
package edu.xcdq.demo01;

import java.io.File;
import java.io.IOException;

public class Demo01 {

public static void main(String[] args) throws IOException {
    // 1 createNewFile()  创建一个文件(父路径必须要存在)

    File file1 = new File("c:/demo01.mp4");
    file1.createNewFile();

...
//2 mkdir()创建以后个文件夹(父路径必须要存在)

    File file2 = new File("c:/demo1");
    boolean res = file2.mkdir();
    System.out.println(res);

...

// 3 mkdirs 创建多个文件夹(父路径不存在,则帮忙创建出来)

    File file3 = new File("c:/demo20/demo2.1");
    boolean res3 = file3.mkdirs();
    System.out.println(res3);

...
// 4 delete()删除文件和空目录

    file1.delete();
    file2.delete();

...
//5 renameTo() 移动文件位置

    File file5 = new File("C:\\demo5.1\\1.txt");
    boolean res5 = file3.renameTo(new File("e:/1_back.txt"));
    System.out.println(res5);

...
// 6 exists() isFile() isDirectory() getPath()
System.out.println(file1.exists());
System.out.println(file2.exists());

    // 7 isFile() isDirectory() getPath()

    System.out.println(file1.isFile());
    System.out.println(file2.isFile());
    System.out.println(file1.isDirectory());
    System.out.println(file2.isDirectory());

...
// 8 getPath()获取路径

    System.out.println(file5.getPath());


}

...
}
...

你可能感兴趣的:(file的常用方法)