“createNewFile | ”方法是做什么的

“createNewFile | ”方法是做什么的

本文目录

  • “createNewFile()”方法是做什么的
  • File FilecreateNewFile和createTempFile的区别
  • java中file类的方法createNewFile()返回值是boolean,为什么可以直接调用,而不用赋值给一个变量
  • java中createNewFile怎么使用
  • android 下载文件的时候 createNewFile 怎么回事java.io.IOException: No such file or directory
  • java的mkdir()为什么不需要捕获异常而createNewFile()需要
  • java中File类的createNewFile()
  • 在java中,createnewfile方法是做什么的什么时候用,谢谢大神
  • createNewFile和createTempFile
  • JAVA里Flie类的creatnewfile与creattempfile有什么不同

“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.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“);

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

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

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

File FilecreateNewFile和createTempFile的区别


public class TestFile1 {

public static void main(String args) {
File f1 = new File(“C:\\abc.txt“);
try {
f1.createNewFile();
System.out.println(f1.getName());
} catch (IOException e) {
e.printStackTrace();
}
}

}

java中file类的方法createNewFile()返回值是boolean,为什么可以直接调用,而不用赋值给一个变量


一般调用createNewFile()这个方法的目的就是创建文件,文件存在与否不是很重要。查看api可以知道当不存在指定文件是会自动创建文件并返回true,当存在指定文件时不会创建该文件并返回false。
还是一句话,用了createNewFile()就比较保险,无论它返回true还是false指定的文件都是会存在地。

java中createNewFile怎么使用


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

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“);//在默认路径创建一个file类
         
         // tries to create new file in the system
         bool = f.createNewFile();//返回true或者false判断该文件是否已经创建好
         
         // 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();
      }
   }
}
让我们编译和运行上面的程序,这将产生以下结果:
File created: false
delete() method is invoked
File created: true

android 下载文件的时候 createNewFile 怎么回事java.io.IOException: No such file or directory


你创建文件的目录不存在啊。首先创建那个目录在创建文件啊。或者直接用FileOutPutStream啊。。
日志已经说明了没有这个目录。如果还搞不定hi我。

java的mkdir()为什么不需要捕获异常而createNewFile()需要


createNewFile() 时可能它的某个父文件夹不存在 比如c:/a/b/1.txt 如果C盘下没有一个文件夹叫 a 的话,就会抛如下异常:
Exception in thread “main“ java.io.IOException: 系统找不到指定的路径。
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at cn.test.Ts.main(Ts.java:13)
因为这个Native Method没有创建父文件夹的功能
调用 someFile.getParentFile().mkdirs()方法就是为了创建这个文件
因为是mkdirs()方法会递归的调用自身:“&& (parent.mkdirs() “ ,直到要创建的文件夹的父文件夹存在:
if (mkdir()) { return true; }
所以不会发生找不到路径的情况 找不到路径我就一直创建,就不需要抛ioexception

java中File类的createNewFile()


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

在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.yiibai;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();}}}

createNewFile和createTempFile


File.createNewFilefile类的createnewfile根据抽象路径创建一个新的空文件,当抽象路径制定的文件存在时,创建失败
File.createTempFile
的用途是你想要建立一个档案暂时使用,但是你不在乎其精确的档案名,只要不覆盖到已存在的档案时。可以制定
临时文件
的文件名前缀、后缀及文件所在的目录,如果不指定目录,则存放在系统的
临时文件夹
下。

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“
返回:
表示新建空文件的抽象路径名

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部