创建新文件函数
例如:
boolean createNewFile = file.createNewFile();
System.out.println(createNewFile);
根据这两行代码可以判断,创建文件时 如果文件不存在则创建文件并返回true,如果文件存在则不再创建并返回false.
在数据库中创建新的文件
方法自动创建此抽象路径名的新文件.文件锁设备应该使用这种方法,文件锁定会导致协议无法进行可靠地工作.
①声明以下是createNewFile()方法的声明:
public boolean createNewFile()
NA
此方法返回true,如果指定的文件不存在,并已成功创建.如果该文件存在,该方法返回false.
IOException -- 如果发生I/ O错误
SecurityException --如果SecurityManager.checkWrite(java.lang.String) 方法拒绝写入权限的文件
下面的示例演示createNewFile()方法的用法.
package com.yiibai;
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("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");
}catch(Exception e){
e.printStackTrace();
}
File file=new File("1.txt");
if(!file.exists())
file.createNewFile();
如果创建的文件名不存在,再创建
以上就是土嘎嘎小编大虾米为大家整理的相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!