Java File.exists()方法

2019-10-16 22:15:58

Java Java File.exists()方法具有以下語法。

public boolean exists()

範例

在下面的程式碼顯示如何使用File.exists()方法。


import java.io.File;
// @  W w  W.Y  iI B A I. C o  M
public class Main {
  public static void main(String[] args) throws Exception{

    // create new files
    File f = new File("test.txt");

    // create new file in the system
    f.createNewFile();

    // tests if file exists
    boolean bool = f.exists();

    System.out.println("File exists: " + bool);

  }
}

上面程式碼執行結果如下 -

File exists: true