导航:首页 > 编程语言 > javacreatenewfile

javacreatenewfile

发布时间:2023-06-26 16:58:00

Ⅰ “createNewFile()”方法是做什么的

方法自动创建此抽象路径名的新文件。文件锁设备应该使用这种方法,文件锁定会导致协议无法进行可靠地工作。
1.声明
以下是createNewFile()方法的声明:
public boolean createNewFile()

2.参数
NA
3.返回值
此方法返回true,如果指定的文件不存在,并已成功创建。如果该文件存在,该方法返回false。
4.异常
IOException -- 如果发生I/ O错误
SecurityException --如果SecurityManager.checkWrite(java.lang.String) 方法拒绝写入权限的文件
5.例子
下面的示例演示createNewFile()方法的用法。
package com.yii;

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

// tries to create new file in the system
bool = f.createNewFile();

// print
System.out.println("File created: "+bool);

}catch(Exception e){
e.printStackTrace();
}
}
}

Ⅱ java中createNewFile怎么使用

java中createNewFile方法主要是如果该文件已经存在,则不创建,返回一个false,如果没有,则返回true,如下代码:

packagecom.yii;

importjava.io.File;

publicclassFileDemo{
publicstaticvoidmain(String[]args){

Filef=null;
booleanbool=false;

try{
//createnewfile
f=newFile("test.txt");//在默认路径创建一个file类

//
bool=f.createNewFile();//返回true或者false判断该文件是否已经创建好

//prints
System.out.println("Filecreated:"+bool);

//deletesfilefromthesystem
f.delete();

//delete()isinvoked
System.out.println("delete()methodisinvoked");

//
bool=f.createNewFile();

//print
System.out.println("Filecreated:"+bool);

}catch(Exceptione){
e.printStackTrace();
}
}
}
让我们编译和运行上面的程序,这将产生以下结果:
Filecreated:false
delete()methodisinvoked
Filecreated:true

Ⅲ 不调用createNewFile()在java中怎么创建一个新的文件

new java.io.FileOutputStream("newFile.txt");

就可以生成文件 了。。。。。。。。

Ⅳ java中File类的createNewFile()

File file=new File("1.txt");
if(!file.exists())
file.createNewFile();
就创建好了,随便什么扩展名都是可以的

Ⅳ java中创建文件

一般都可以通过”new file“的形式来完成文件创建。代码如下:
import java.io.*;
public class filename {
public static void main(String[] arg) throws IOException { //以下操作可能出现异常,必须放入try块中
try{
File path=new File("F:\\filepath"); //先设置文件路径
File dir=new File(path,"filename.txt"); //设置在文件路径下创建新文件的名称
if(!dir.exists()) //判断文件是否已经存在
dir.createNewFile(); //如果不存在的话就创建一个文件
}
catch(Exception e){ //如果存在就会报错,
System.out.print("创建失败");//输出创建失败信息,也就证明当前要创建的文件已经存在。
}
}
}

阅读全文

与javacreatenewfile相关的资料

热点内容
pdf图片背景 浏览:766
app的图标有什么风格 浏览:28
python代码运行编译器 浏览:936
魔鬼训练程序员 浏览:686
php上传大文件失败 浏览:602
sw服务器指定端口怎么填 浏览:189
java有哪些数组 浏览:984
程序员戴手表影响工作吗 浏览:235
游戏皇后解压视频 浏览:367
c语言怎么打开文件编译 浏览:436
手机上什么app可以设计logo 浏览:800
pid算法单片机 浏览:375
python数据精度 浏览:632
管什么小女孩App 浏览:192
phppdf转换成图片 浏览:468
十八讲pdf 浏览:619
mysql导入压缩文件 浏览:22
usb控制单片机 浏览:906
你为什么喜欢安卓手机 浏览:863
阿里云服务器购买和使用 浏览:389