File类的createNewFile()方法为什么抛出了异常?
import java.io.*;
class FileCons {
public static void main(String[] args) {
try{
File f1 = new File( "d:\\ ");
File f2 = new File( "d:\\ ", "zhaozhao ");
File f3 = new File( "f2 ", "sss.txt ");
File f4 = new File( "File1.java ");
File f5 = new File( "asdf+?12/ ");
if (f1.exists())
System.out.println(f1 + " exists ");
else System.out.println(f1 + " is not exists ");
System.out.println( "Path of F1 is " + f1.getPath());
if (f2.exists())
System.out.println(f2 + " exists ");
else System.out.println(f2 + " is not exists ");
/*从答案中我们可以看出d:\zhaozhao这个文件夹并不存在
/*下面我们将创建文件夹zhaozhao*/
f2.mkdir() ;
if (f2.exists())
System.out.println( "After mkdir() , "+f2 + " exists ");
else System.out.println(f2 + " is still not exists ");
if (f3.exists())
System.out.println(f3 + " exists ");
else { System.out.println(f3 + "is not exists "); /*由这句话我们知道f3.exits()
没有抛出异常*/
f3.createNewFile();
/*d:\zhaozhao文件夹已经存在,为什么仍然无法在那个文件夹下创建文件sss.txt,
*f3.createNewFile()抛出异常*/
System.out.println( "F3 was created! ");
}
if(f4.exists())
{ System.out.println(f4+ " exists,the AbsolutePath is "+f4.getAbsolutePath());
}
else{
f4.createNewFile();