Delete and create File files and directories

FIle knowledge point three

File creation

boolean createNewFile(), can only create a file, can not create a path where the folder exists to create a new file

Code demonstration (here is the method!)

    private static void show01() throws IOException {
    File f1 = new File("E:\\xpu\\ideaproject\\20190905\\src\\cn\\itcast\\File\\1.txt");
    boolean b1 = f1.createNewFile();
    System.out.println(b1);//文件不存在,创建一个新的1.txt的文件,为true。//文件存在了,为false
    //相对路径存一个新文件
    File f2 = new File("src\\cn\\itcast\\File\\2.txt");
    System.out.println(f2.createNewFile());//true文件存在了,为false
    File f3= new File("src\\cn\\itcast\\File\\新建文件夹");
    System.out.println(f3.createNewFile());//true,存进去的不是文件夹形式,而是文件,不要被名称所迷惑,要看类型
    File f4= new File("src\\cn\\itcast\\Fil\\新建文件夹");
    System.out.println(f4.createNewFile());//不存在的路径抛出异常
 }

Points to note:
1. All created are files, no matter how they are named!
2. If the file exists, it returns false, or the path path is incorrect and the exception information is returned!

Catalog creation

Note:
1. There is a big difference between folder creation and file creation. If you need to create a single-level folder, you need to create it in the root directory (relative path), using the mkdir method, see f5 and b5 for details.
2. If you need to create in the specified folder, that is to say it is not created in the first-level directory, you need to call the mkdirs method, see the f7 and b7
methods in detail :
boolean mkdir() creates a single-level empty folder, only the relative path Create next! You can only create a folder, not a file
. If the folder does not exist, create a new folder and return true. If the folder exists, it will not be created. Return false

Demo code (method):

private static void show02() {
    File f1 = new File("src\\新建文件夹");
    boolean b1 = f1.mkdir();
    System.out.println(b1);//true//再次创建为false
    File f2 = new File("src\\111\\222\\333");
    boolean b2 = f2.mkdir();
    System.out.println(b2);//false,不能创建多级文件夹
    File f4 = new File("src\\111.txt");
    boolean b4 = f4.mkdir();
    System.out.println(b4);//true,创建一个名为111.txt的文件夹
    File f5 = new File("sr");
    boolean b5 = f5.mkdir();
    System.out.println(b5);//true
    //boolean mkdirs()既可以创建单级空文件夹,又可以创建多级空文件夹
    File f3 = new File("src\\111\\222\\333");
    boolean b3 = f3.mkdirs();
    System.out.println(b3);//true
    File f6 = new File("sr\\111");
    boolean b6 = f6.mkdirs();
    System.out.println(b6);//true,创建了文件夹
    File f7 = new File("src\\cn\\itcast\\File\\123");
    boolean b7 = f7.mkdirs();
    System.out.println(b7);//true

}

Delete files and directories

Delete the file or directory represented by this File
This method: You can delete the file/folder given in the construction method path
Return value: Boolean
true: The file/folder is deleted successfully
false: There is content in the folder and will not be deleted. Return false, the path of the construction method does not exist false
Note: The
delete method is to delete files and folders directly from the hard disk. Do not go to the recycle bin. Be careful when deleting! */
boolean delete(); delete
relative path

Code demo:

private static void show03() {
   // boolean delete();
    //相对路径的删除
    File f1 = new File("src\\新建文件夹");
    boolean b1 = f1.delete();
    System.out.println(b1);//true
   //绝对路径的删除不行,换抽象路径
    File f2 = new File("sr\\111");
    boolean b2 = f2.delete();
    System.out.println(b2);//true
    //有里往外顺序删除
    File f3 = new File("sr");
    boolean b3 = f3.delete();
    System.out.println(b3);//true
    File f4 = new File("src/cn/itcast/File/新建文件夹");
    boolean b4 = f4.delete();
    System.out.println(b4);//true
    //里面有内容的文件夹
    File f5 = new File("src\\111");
    boolean b5 = f5.delete();
    System.out.println("b5"+b5);//false
} 

Important note:
file creation can use both absolute path and relative path.
The creation of a directory must use a relative path to
delete both. It needs to be done under a relative path. The deletion must be done with caution, because it is directly operated on the hard disk. , The risk factor is greater!

Guess you like

Origin blog.csdn.net/tangshuai96/article/details/102749252