Java File.createNewFile()方法

2019-10-16 22:15:51

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

public boolean createNewFile()   throws IOException

範例

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


import java.io.File;

public class Main {
  public static void main(String[] args) throws Exception{
    File f = new File("test-nf.txt");// Copyright: W w  w. yi Ib Ai .C O  M

    // tries to create new file in the system
    boolean bool = f.createNewFile();

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

  }
}

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

File created: false