- 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)