s-day08-File中的创建功能
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
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 {
|
||||||
|
//public boolean createNewFile() :当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。(重点)
|
||||||
|
File f = new File("a.txt");
|
||||||
|
if (!f.exists()) {
|
||||||
|
System.out.println(f.createNewFile());
|
||||||
|
}
|
||||||
|
System.out.println("--------------");
|
||||||
|
File f1 = new File("b.txt");
|
||||||
|
if (!f1.exists()) {
|
||||||
|
System.out.println(f1.mkdir());
|
||||||
|
}
|
||||||
|
System.out.println("--------------");
|
||||||
|
File f2 = new File("a/b");
|
||||||
|
if (!f2.exists()) {
|
||||||
|
// System.out.println(f2.mkdir());//mkdir:必定要父路径存在,才能创建
|
||||||
|
System.out.println(f2.mkdirs());//mkdirs:如果父路径不存在,顺便把父路径一起创建出来
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user