The use of "." or not for current directory, in Java

Just got one exception when I wanted to use a local file inside a local directory:

File f = new File("/data/a.xml");
FileReader r = new FileReader(f);

I got "java.io.FileNotFoundException". I used "/data/a.xml" directly because I saw some one use it the similar way. I thought I might ignore the use of "." to point to the current directory. It seems do not work. I have to add it back again. Then the code below works:

File f = new File("./data/a.xml");
FileReader r = new FileReader(f);

------------------------------------------

Then I check that guy's code again, and I found the difference. He is in fact using:

File f = new File("data/a.xml");
FileReader r = new FileReader(f);

-------------------------------------------

Now I know, I should either use dot this way: "./d1/d2/f" or "d1/d2/f" when I want to access a file f located in my current work directory. Great.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章