16

Javaプログラムで、ファイルを作成します

File temp = new File("temp");
temp.createNewFile();

そしたら何故か書いてると

File pDir = temp.getParentFile();

pDir は null です。本当は書きたい

File pDir = temp.getParentFile().getParentFile();

しかし、それはヌルポインタ例外をスローします。

4

2 に答える 2

0

You're creating a file called temp, but it has no path, so there will be no parent path. If you want to put the file in the current directory:

File temp = new File(System.getProperty("user.dir")+"/temp");
File parent = temp.getParentFile();
于 2012-07-02T15:52:16.693 に答える