Simple Sand Samples |
Java > ファイルやディレクトリの存在を調べる
exists.java
import java.io.File; class Test{ public static void main(String args[]) { File f = new File("hoge.txt"); if(f.exists()){ System.out.println("Found."); }else{ System.out.println("Not Found."); } } }
$ javac exists.java
$ java Test
Not Found.
$ touch hoge.txt
$ java Test
Found.
$
$ java Test
Not Found.
$ touch hoge.txt
$ java Test
Found.
$
* 確認環境 Debian Linux(Wheezy)