导航:首页 > 编程语言 > 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相关的资料

热点内容
sql数据库查询表命令 浏览:551
简单音乐网站源码 浏览:644
运动健康app华为手表怎么连接 浏览:748
肌肉塑造全书pdf下载 浏览:796
安卓简约拼图用什么软件好 浏览:289
fx1n加密程序 浏览:844
淘客阿里云服务器 浏览:476
100压缩打造 浏览:422
安卓手机怎么和苹果平板传文件 浏览:973
开始选项卡中的页眉和页脚命令选项 浏览:424
pdf的字体怎么改 浏览:856
python读写视频 浏览:88
科鲁兹压缩机轴承 浏览:353
word文档转换成pdf文件找不到 浏览:27
组件注册命令 浏览:760
安卓大屏导航用的是什么运放 浏览:443
myandroidtools的备份 浏览:900
python爬虫天气预报 浏览:761
android70权限管理 浏览:749
魔兽辛迪加是什么服务器 浏览:472