百度
360搜索
搜狗搜索

createnewfile,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类中file.mkdir()和file.mkdirs()与file.createNewFile()

1、file.mkdir() 如果你想在已经存在的文件夹下建立新的文件夹,就可以用此方法。此方法不能在不存在的文件夹下建立新的文件夹。

2、file.mkdirs() 如果你想根据File里的路径名建立文件夹(当你不知道此文件夹是否存在,也不知道父文件夹存在),就可用此方法,它建立文件夹的原则是:如果父文件夹不存在并且最后一级子文件夹不存在,它就自动新建所有路经里写的文件夹;如果父文件夹存在,它就直接在已经存在的父文件夹下新建子文件夹。

3、createNewFile文件不存在则创建,存在则不创建并返回false,文件路径必须存在才可创建路径下的文件(注意它只能创建文件,即如果你给了/storage/emulated/0/hello/snow/这样一个路径,它最后也只是在hello文件夹中创建了snow的未知文件而不是文件夹,如上所述的创建成功的前提还是要/storage/emulated/0/hello/这样的文件夹路径存在,如果只有/storage/emulated/0这样的文件夹路径,它是不能够创建hello文件夹的,所以创建失败)

4、mkdirs()和mkdirs()专门用来创建文件夹的,不存在则创建返回true,存在则返回false,区别在于mkdirs可以creating missing parent directories if necessary.同样的路径为/storage/emulated/0/hello/snow.bin也只是在hello文件夹啊下创建了snow.bin文件夹而不是文件。

5、所以一般需要createNewFile()和mkdirs()结合使用,先创建文件夹再创建文件。

java中createNewFile怎么使用?

如果创建的文件名不存在,再创建
File file=new File("1.txt");
if(!file.exists())
file.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: falsedelete() method is invokedFile created: 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"
返回:
表示新建空文件的抽象路径名

java中File类的createNewFile()

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

Linux系统下,用File 里的createNewFile()创建文件是中文名,文件名为一堆问号

搜一下:Linux系统下,用File
里的createNewFile()创建文件是中文名,文件名为一堆问号
createNewFile()创建时用中文一定是乱码,因为它是以二进制形式进行写入的.而touch命令是直接对用户的输入进行直译写入操作,不会转换格式,所以显示正常. 这种并无解决办法,其实linux应用方面建议还是多用英文,linux目前还是存在很多编码转换问题,毕竟linux系统就是用英文编写的,对中文的支持上迄今只在界面话上有所提高,终端的操作还是少用中文吧.

eclipsecreateNewfile时nofolderselected是什么意思

eclipse创建新文件时未选择文件夹。eclipse食;黯然失色;羽毛暗淡期;遮掩?的光;遮暗;遮掉;掩没?的重要性;使黯然失色create创造;创作;创建;大惊小怪;抱怨;导致;引起;封;引发;授予new新的;新接触的;全新的;更好的;崭新的;新型的;更新的;新出现的;另外的;附加的;新;新近file文件夹;文件箱;纵列;锉刀;文件;档案;卷宗;案卷;职责;把?存档;列队行进;鱼贯而行;锉;把?锉平;把?锉成形;提交;提起;向报社(或新闻机构)发送folder文件夹;纸夹;文件夹图标;折叠式传单selected挑选;选拔;(select的过去式和过去分词);挑选出来的;精心挑选的?

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判断文件是否存在不存在就创建

if(!file.exist()){
file.createNewFile();
}
用File类中的.exists()方法判断是否存在
mkdirs创建目录
createNewFile()创建文件
多看看API文档
boolean
exists()
测试此抽象路径名表示的文件或目录是否存在。
createNewFile()
当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
boolean
mkdirs()
创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。

Java创建文件夹及文件

  package xj util;
  import java io File;
  import java io IOException;
  public class CreateFileUtil {
  public static boolean CreateFile(String destFileName) {

  File file = new File(destFileName);
  if (file exists()) {
  System out println( 创建单个文件 + destFileName + 失败 目标文件已存在! );
  return false;
  }
  if (destFileName endsWith(File separator)) {
  System out println( 创建单个文件 + destFileName + 失败 目标不能是目录! );
  return false;
  }
  if (!file getParentFile() exists()) {
  System out println( 目标文件所在路径不存在 准备创建 );
  if (!file getParentFile() mkdirs()) {
  System out println( 创建目录文件所在的目录失败! );
  return false;
  }
  }
  // 创建目标文件
  try {
  if (file createNewFile()) {
  System out println( 创建单个文件 + destFileName + 成功! );
  return true;
  } else {
  System out println( 创建单个文件 + destFileName + 失败! );
  return false;
  }
  } catch (IOException e) {
  e printStackTrace();
  System out println( 创建单个文件 + destFileName + 失败! );
  return false;
  }
  }
  public static boolean createDir(String destDirName) {
  File dir = new File(destDirName);
  if(dir exists()) {
  System out println( 创建目录 + destDirName + 失败 目标目录已存在! );
  return false;
  }
  if(!destDirName endsWith(File separator))
  destDirName = destDirName + File separator;
  // 创建单个目录
  if(dir mkdirs()) {
  System out println( 创建目录 + destDirName + 成功! );
  return true;
  } else {
  System out println( 创建目录 + destDirName + 成功! );
  return false;
  }
  }
  public static String createTempFile(String prefix String suffix String dirName) {
  File tempFile = null;
  try{
  if(dirName == null) {
  // 在默认文件夹下创建临时文件
  tempFile = File createTempFile(prefix suffix);
  return tempFile getCanonicalPath();
  }
  else {
  File dir = new File(dirName);
  // 如果临时文件所在目录不存在 首先创建
  if(!dir exists()) {
  if(!CreateFileUtil createDir(dirName)){
  System out println( 创建临时文件失败 不能创建临时文件所在目录! );
  return null;
  }
  }
  tempFile = File createTempFile(prefix suffix dir);
  return tempFile getCanonicalPath();
  }
  } catch(IOException e) {
  e printStackTrace();
  System out println( 创建临时文件失败 + e getMessage());
  return null;
  }
  }
  public static void main(String[] args) {
  // 创建目录
  String dirName = c:/test/test /test ;
  CreateFileUtil createDir(dirName);
  // 创建文件
  String fileName = dirName + /test /testFile txt ;
  CreateFileUtil CreateFile(fileName);
  // 创建临时文件
  String prefix = temp ;
  String suffix = txt ;
  for(int i = ; i < ; i++) {
  System out println( 创建了临时文件: + CreateFileUtil createTempFile(prefix suffix dirName));
  }
  }
lishixinzhi/Article/program/Java/hx/201311/25690

阅读更多 >>>  linux系统怎么修改文件类型

网站数据信息

"createnewfile,JAVA里Flie类的creatnewfile与creattempfile有什么不同"浏览人数已经达到20次,如你需要查询该站的相关权重信息,可以点击进入"Chinaz数据" 查询。更多网站价值评估因素如:createnewfile,JAVA里Flie类的creatnewfile与creattempfile有什么不同的访问速度、搜索引擎收录以及索引量、用户体验等。 要评估一个站的价值,最主要还是需要根据您自身的需求,如网站IP、PV、跳出率等!