Ⅰ java中careatenewfile方法是什么意思
File.createNewFilefile类的createnewfile根据抽象路径创建一个新的空文件,当抽象路径制定的文件存在时,创建失败
File.createTempFile 的用途是你想要建立一个档案暂时使用,但是你不在乎其精确的档案名,只要不覆盖到已存在的档案时。可以制定临时文件的文件名前缀、后缀及文件所在的目录,如果不指定目录,则存放在系统的临时文件夹下。
Ⅱ java中file类的方法createNewFile()返回值是boolean,为什么可以直接调用,而不用赋值给一个变量
一般调用createNewFile()这个方法的目的就是创建文件,文件存在与否不是很重要。查看api可以知道当不存在指定文件是会自动创建文件并返回true,当存在指定文件时不会创建该文件并返回false。
还是一句话,用了createNewFile()就比较保险,无论它返回true还是false指定的文件都是会存在地。
Ⅲ 不调用createNewFile()在java中怎么创建一个新的文件
new java.io.FileOutputStream("newFile.txt");
就可以生成文件 了。。。。。。。。
Ⅳ 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();
System.out.println("Filecreated:"+bool);
}catch(Exceptione){
e.printStackTrace();
}
}
}
让我们编译和运行上面的程序,这将产生以下结果:
Filecreated:false
delete()methodisinvoked
Filecreated:true
Ⅳ JAVA里Flie类的creatnewfile与creattempfile有什么不同
后者的文件建立在默认的临时文件目录中 不在当前目录
createNewFile
public boolean createNewFile()
throws IOException当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。检查文件是否存在,若不存在则创建该文件,这是单个操作,对于其他所有可能影响该文件的文件系统活动来说,该操作是不可分的。
createTempFile
public static File createTempFile(String prefix,
String suffix)
throws IOException在默认临时文件目录中创建一个空文件,使用给定前缀和后缀生成其名称。调用此方法等同于调用 createTempFile(prefix, suffix, null)。
参数:
prefix - 用于生成文件名的前缀字符串;必须至少是三字符长
suffix - 用于生成文件名的后缀字符串;可以为 null,在这种情况下,将使用后缀 ".tmp"
返回:
表示新建空文件的抽象路径名
Ⅵ android 下载文件的时候 createNewFile 怎么回事java.io.IOException: No such file or directory
你创建文件的目录不存在啊。首先创建那个目录在创建文件啊。或者直接用FileOutPutStream啊。。
日志已经说明了没有这个目录。如果还搞不定hi我。
Ⅶ JAVA createNewFile()方法疑问
异常
file.createNewFile();
这句话会有异常抛出 因为你虽然知道word.txt是不存在的
但是程序不会提前知道的 所以就会抛出一个异常
Ⅷ java中File类的createNewFile()
File file=new File("1.txt");
if(!file.exists())
file.createNewFile();
就创建好了,随便什么扩展名都是可以的
Ⅸ “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方法是做什么的什么时候用,谢谢大神
方法自动创建此抽象路径名的新文件。文件锁设备应该使用这种方法,文件锁定会导致协议无法进行可靠地工作。1.声明以下是createNewFile()方法的声明:public boolean createNewFile()2.参数NA3.返回值此方法返回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 filef = new File("test.txt");// tries to create new file in the systembool = f.createNewFile();// printsSystem.out.println("File created: "+bool);// deletes file from the systemf.delete();// delete() is invokedSystem.out.println("delete() method is invoked");// tries to create new file in the systembool = f.createNewFile();// printSystem.out.println("File created: "+bool);}catch(Exception e){e.printStackTrace();}}}