网站首页 > 文章中心 > 其它

createNewFile_createnewfile方法的作用

作者:小编 更新时间:2023-10-04 18:25:33 浏览量:135人看过

创建新文件函数

例如:

boolean createNewFile = file.createNewFile();

System.out.println(createNewFile);

根据这两行代码可以判断,创建文件时 如果文件不存在则创建文件并返回true,如果文件存在则不再创建并返回false.

在数据库中创建新的文件

createNewFile_createnewfile方法的作用-图1

方法自动创建此抽象路径名的新文件.文件锁设备应该使用这种方法,文件锁定会导致协议无法进行可靠地工作.

①声明

以下是createNewFile()方法的声明:

public boolean createNewFile()

NA

createNewFile_createnewfile方法的作用-图2

此方法返回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");

// print

}catch(Exception e){

e.printStackTrace();

}

createNewFile_createnewfile方法的作用-图3

File file=new File("1.txt");

if(!file.exists())

file.createNewFile();

如果创建的文件名不存在,再创建

以上就是土嘎嘎小编大虾米为大家整理的相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章