进阶day08-File中的创建功能
This commit is contained in:
30
javaSE-day08/src/com/inmind/file01/FileDemo05.java
Normal file
30
javaSE-day08/src/com/inmind/file01/FileDemo05.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.inmind.file01;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
File中的创建功能
|
||||||
|
public boolean createNewFile() :当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。(重点)
|
||||||
|
public boolean mkdir() :创建由此File表示的目录。
|
||||||
|
public boolean mkdirs() :创建由此File表示的目录,包括任何必需但不存在的父目录。(重点)
|
||||||
|
*/
|
||||||
|
public class FileDemo05 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
File file = new File("a.txt");
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.createNewFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
File file1 = new File("b.txt");
|
||||||
|
if (!file1.exists()) {
|
||||||
|
file1.mkdir();
|
||||||
|
}
|
||||||
|
|
||||||
|
File file2 = new File("b/a");
|
||||||
|
if (!file2.exists()) {
|
||||||
|
//如果想要将对应不存在的父路径(b文件夹)也一次性地创建出来,那么必须使用mkdirs方法
|
||||||
|
file2.mkdirs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user