java file 不存在_Java FileOutputStream如果不存在则创建文件

Java FileOutputStream如果不存在则创建文件

有没有办法以一种方式使用FileOutputStream,如果文件(String filename)不存在,那么它会创建它吗?

FileOutputStream oFile = new FileOutputStream("score.txt", false);

8个解决方案

237 votes

如果文件不存在且无法创建(doc),它将抛出FileNotFoundException,但如果可以的话,它将创建它。 为了确保在创建FileOutputStream之前可能首先应该测试该文件是否存在(如果没有,则创建为createNewFile()):

File yourFile = new File("score.txt");

yourFile.createNewFile(); // if file already exists will do nothing

FileOutputStream oFile = new FileOutputStream(yourFile, false);

talnicolas answered 2019-05-11T09:50:48Z

25 votes

在创建文件之前,需要创建所有父目录。

使用yourFile.getParentFile().mkdirs()

Kostiantyn Medvid answered 2019-05-11T09:51:21Z

20 votes

您可以创建一个空文件,无论它是否存在......

new FileOutputStream("score.txt", false).close();

如果你想保留文件,如果它存在...

new FileOutputStream("score.txt", true).close();

如果尝试在不存在的目录中创建文件,则只会获得FileNotFoundException。

Peter Lawrey answered 2019-05-11T09:52:02Z

20 votes

File f = new File("Test.txt");

if(!f.exists()){

f.createNewFile();

}else{

System.out.println("File already exists");

}

将此f传递给您的FileOutputStream构造函数。

Shashank Kadne answered 2019-05-11T09:52:27Z

13 votes

来自apache commons的FileUtils是一个很好的方法,可以在一行中实现这一点。

FileOutputStream s = FileUtils.openOutputStream("/home/nikhil/somedir/file.txt")

这将创建父文件夹(如果不存在)并创建文件(如果不存在)并在文件对象是目录或无法写入时抛出异常。 这相当于:

File file = new File("/home/nikhil/somedir/file.txt");

file.getParentFile().mkdirs(); // Will create parent directories if not exists

file.createNewFile();

FileOutputStream s = new FileOutputStream(file,false);

如果不允许当前用户执行操作,则上述所有操作都将抛出异常。

Nikhil Sahu answered 2019-05-11T09:53:08Z

3 votes

如果不存在则创建文件。 如果文件存在,请清除其内容:

/**

* Create file if not exist.

*

* @param path For example: "D:\foo.xml"

*/

public static void createFile(String path) {

try {

File file = new File(path);

if (!file.exists()) {

file.createNewFile();

} else {

FileOutputStream writer = new FileOutputStream(path);

writer.write(("").getBytes());

writer.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

Do Nhu Vy answered 2019-05-11T09:53:35Z

1 votes

如果文件不存在,您可能会获得FileNotFoundException。

Java文档说:

文件是否可用或是否可以创建取决于   底层平台  [http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html]

如果您使用的是Java 7,则可以使用java.nio包:

options参数指定文件的创建或打开方式...它打开文件进行写入,如果文件不存在则创建文件...

[http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html]

sikander answered 2019-05-11T09:54:38Z

1 votes

只有在使用Path和Files不存在的情况下,才提供另一种创建文件的方法。

Path path = Paths.get("Some/path/filename.txt");

Files.createDirectories(path.getParent());

if( !Files.exists(path))

Files.createFile(path);

Files.write(path, ("").getBytes());

Manish Bansal answered 2019-05-11T09:55:05Z

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值