范例:操作文件
·创建文件:public boolean createNewFile( )throws IOException;
|-如果目录不能访问;
|-如果现在文件重名,或者是文件名称错误;
·删除文件:public boolean delete( );
·判断文件是否存在:public boolean exists()
import java.io.File;
public class Demo {
public static void main(String[] args) throws Exception {
File file = new File("G:" + File.separator + "Test.txt");
if (file.exists()) {// 文件是否存在
file.delete();// 如果存在则删除
} else {
System.out.println(file.createNewFile());// 如果文件不存在则创建文件并返回boolean值
}
}
}
