
- Java.io 包類
- Java.io - 首頁
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io 包其他內容
- Java.io - 介面
- Java.io - 異常
- Java.io 包有用資源
- Java.io - 討論
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)