有时候,别人觉得很简单的东西,初学者却是毫无概念的。
读李老师IO一章开头P696的例子时,一脸的茫然,没有丝毫的感觉。看第一个语句 File file = new File(".");,就很不明白为什么 new 了一个 File 后,文件夹却找不到任何这个文件的痕迹,而之后的 System.out.println(file.getAbsoluteFile());却大摇大摆的输出 C:\workspace-StudyJava\Exercises\.,偶自做聪明的把" . "改成了"a",依然找不到这个文件. 感觉这输出很有睁眼说瞎话嫌疑挖,嘎嘎
Annie "名言": Don't forget the magic word: google. 好了,google下下: "how to create a file in java",找到了:
import java.io.*;
public class CreateFile1{
public static void main(String[] args) throws IOException{
File f;
f=new File("myfile.txt");
if(!f.exists()){
f.createNewFile();
System.out.println("New file \"myfile.txt\" has been created
to the current directory");
}
}
}
Why??? kind of stupid right
~~
OK,写个程序验证下下
import java.io.*;
public class FileTest {
public static void main(String[] args) throws IOException
{
File file = new File("a");
System.out.println(file.getName()); //output:a
System.out.println(file.getParent()); //output:null
System.out.println(file.getAbsoluteFile()); //C:\workspace-StudyJava\Exercises\a
System.out.println(file.getAbsoluteFile().getParent());//C:\workspace-StudyJava\Exercises
System.out.println("Object file exists?" + file.exists()); //output:false
file.createNewFile();
System.out.println("Object file exists after createNewFile?" + file.exists());//output:true
}
}