Java"new File()";不创建文件 [英] Java "new File()" does not create file

查看:568
本文介绍了Java"new File()";不创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是一个非常愚蠢的问题,但我似乎无法在Java中创建新文件来挽救生命.

This will be a really dumb question, but i can't seem to create a new file in java to save my life.

它总是抛出

java.io.FileNotFoundException:用户/用户名/Documents/testProject/test.txt(无此类文件或目录)

java.io.FileNotFoundException: Users/username/Documents/testProject/test.txt (No such file or directory)

我尝试过这样:

File newFile = new File("Users/username/Documents/testProject/test.txt");

并尝试了此操作:

File newFile = new File("/Users/username/Documents/testProject/test.txt");

我在做什么错了?

显然问题不存在.抱歉,我稍后尝试在代码中读取一个空文件.

apparently the issue wasn't there. I was trying to read from an empty file later on in the code, sorry folks.

推荐答案

new File("...")不会创建新文件.它将创建一个包含文件名的新对象(在内存中).然后,您可以对其执行 exists() canRead() isDirectory()之类的操作,并且可以调用 createNewFile()从中创建实际文件.

new File("...") does not create a new file. It creates a new object (in memory) containing a filename. You can then perform operations like exists(), canRead() and isDirectory() on it, and you can invoke createNewFile() to create an actual file out of it.

这篇关于Java"new File()";不创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆