Java - File createNewFile() 方法



描述

Java File createNewFile() 方法以原子方式建立一個由該抽象路徑名命名的新的檔案。對於檔案鎖定,應使用 FileLock 功能而不是此方法,因為生成的協議無法可靠地工作。

宣告

以下是 java.io.File.createNewFile() 方法的宣告:

public boolean createNewFile()

引數

返回值

如果名為檔案不存在並且已成功建立,則此方法返回 true。如果檔案存在,則該方法返回 false。

異常

  • IOException - 如果發生 I/O 錯誤

  • SecurityException - 如果 SecurityManager.checkWrite(java.lang.String) 方法拒絕對檔案的寫入訪問許可權

示例 1

以下示例顯示了 Java File createNewFile() 方法的使用。我們建立了一個 File 引用。然後我們使用系統中不存在的 test.txt 建立 File 物件。使用 createNewFile() 方法,我們建立檔案並列印結果。現在使用 delete() 方法,我們刪除檔案,然後再次嘗試建立檔案並列印結果。

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      boolean bool = false;
      
      try {
         // create new file
         f = new File("F://test.txt");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // prints
         System.out.println("File created: "+bool);
         
         // deletes file from the system
         f.delete();
         
         // delete() is invoked
         System.out.println("delete() method is invoked");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // print
         System.out.println("File created: "+bool);
            
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

File created: true
delete() method is invoked
File created: true

示例 2

以下示例顯示了 Java File createNewFile() 方法的使用。我們建立了一個 File 引用。然後我們使用現在存在於系統中的 test.txt 建立 File 物件。使用 createNewFile() 方法,我們建立檔案並列印結果。現在使用 delete() 方法,我們刪除檔案,然後再次嘗試建立檔案並列印結果。

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      boolean bool = false;
      
      try {
         // create new file
         f = new File("F://test.txt");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // prints
         System.out.println("File created: "+bool);
         
         // deletes file from the system
         f.delete();
         
         // delete() is invoked
         System.out.println("delete() method is invoked");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // print
         System.out.println("File created: "+bool);
            
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

File created: false
delete() method is invoked
File created: true

示例 3

以下示例顯示了 Java File createNewFile() 方法的使用。我們建立了一個 File 引用。然後我們使用 test.txt 建立 File 物件,該物件由於位於受限位置而無法建立。使用 createNewFile() 方法,我們建立檔案並列印結果。現在使用 delete() 方法,我們刪除檔案,然後再次嘗試建立檔案並列印結果。

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      boolean bool = false;
      
      try {
         // create new file
         f = new File("C://Windows/test.txt");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // prints
         System.out.println("File created: "+bool);
         
         // deletes file from the system
         f.delete();
         
         // delete() is invoked
         System.out.println("delete() method is invoked");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // print
         System.out.println("File created: "+bool);
            
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

java.io.IOException: Access is denied
	at java.base/java.io.WinNTFileSystem.createFileExclusively(Native Method)
	at java.base/java.io.File.createNewFile(File.java:1035)
	at FileDemo.main(FileDemo.java:13)
java_file_class.htm
廣告