本篇博客详解Java.io.File类库中的createNewFile()方法
public class CreateNewFileTest {
public staticvoid main(String[] args) {
File f = null;
boolean bool = false;
try {
f =new File("study.txt");
bool = f.createNewFile();
System.out.println("file Exist?" +bool);
bool = f.delete();
System.out.println("file Exist?" +bool);
bool = f.exists();
System.out.println("file Exist?" +bool);
} catch (IOExceptione) {
e.printStackTrace();
}
}
}
输出:
file Exist?true
file Exist?true
file Exist?false
解析:
File file = new File("xx"); 表示某路径下的文件
createNewFile()返回boolean,表示是否创建文件成功
file.delete()返回boolean,表示删除文件成功
f.exists() 返回boolean,表示文件是否存在