new一个File对象,即在磁盘中创建一个文件?
当我新建一个文件对象时,我发现磁盘中没有创建文件,所以我猜文件对象不等于磁盘文件,但是当我向 File
对象写入内容时通过流,我发现文件被创建在磁盘中。
那么,我可以这样想吗,new File()
- 不会在磁盘中创建真正的文件,它只是内存中的一个对象。但是,当您通过流向 File
写入内容时,例如:
FileWrite stream = new FileWrite(file);
stream.write(string);
..当文件不存在时,流将创建一个新文件(可能是函数 steam.write()
这是吗?)?
when I new a File Object ,I found that there is not a file be create in disk,so I guess a File Obeject is not equal to a disk file, but when I write something to the File
object through stream, I found the file be created in disk.
So, can I think like this, new File()
- does not create a real file in disk, it is just an object in ram. But when you write something to the File
through stream, for example:
FileWrite stream = new FileWrite(file);
stream.write(string);
..the stream will create a new file when the file does not exist (maybe function steam.write()
does this?)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FileWriter 根据需要创建或截断文件。写在里面放了一些东西。 File 是一个文件路径名,可能存在也可能不存在。例如 File.exists() 并不总是 true 并且 File.delete() 可以删除文件(即文件不再存在)
FileWriter creates or truncates the file as required. The write put something in it. File is a file path name which may or may not exist. e.g. File.exists() is not always true and File.delete() can delete a file (i.e. the file no longer exists)
File# 怎么样? createNewFile()
?如果您使用的是 Java 7,还可以使用Files.createFile(Path)
,如 Java 教程中的示例。How about
File#createNewFile()
? If you're using Java 7, you can also useFiles.createFile(Path)
, as in this example from the Java tutorial.