使用 java.io.File 类
当您在java程序中创建File对象实例时,并发运行的程序是否也可以访问该文件进行写入?就像我有一个带有 example.txt 路径的 File 对象一样,当我的 File 对象存在时,另一个程序可以在该 example.txt 文件中写入吗?
When you make a File object instance in a java program, is it possible for a concurrently running program to also have access to the file for writing? So like if I had a File object with a path to example.txt, can another program be writing in that example.txt file while I have my File object existing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
执行此操作
对文件没有任何作用。因此任何其他线程或进程都可以根据需要打开该文件。
它只是创建一个代表该文件的对象,但不会打开它或以其他方式触摸它。
Doing
does nothing to the file. So any other thread or process can open the file if they like.
It just creates an object that represents the file, but doesn't open it or otherwise touch it.
如果你已经写了,
那就意味着你只创建了一个文件对象,但没有根据给定的路径在硬盘上创建文件。而且该文件对象位于虚拟内存(Java)中。如果任何其他应用程序已经加载虚拟内存可以访问或调用文件对象的引用,然后应用程序可以访问文件对象。
如果您使用以下命令创建文件,
那么您的硬盘中就有一个真实的文件。然后任何其他应用程序都可以像访问硬盘中的其他文件一样访问它。您会看到此处
IF you have written,
that means, you only created a file object, but not created a file on your harddisk according to a given path.Also that file object is in virtual memory(Java).If any other application which has already loaded in virtual memory can access or call the file object's reference ,then that application can access the file object.
If you create a file with,
Then there is a real file in your hard disk.Then any other application can access it as other files in your hard disk. Would you have see here
是的,因为 Java 中的 File 对象实际上只代表文件系统路径。它实际上并不是一个对底层资源具有锁定语义的文件句柄。您甚至可以为不存在的资源创建 File 实例。
Yes, because a File object in Java really just represents a file system path. It's not actually a file handle that has lock semantics on the underlying resource. You can even create File instances to non-existent resources.
是的。在调用 File 实例的方法之一之前,拥有 File 实例与实际文件系统几乎没有关系。事实上,您可以创建甚至不存在的路径的 File 实例。
Yes. Having a File instance has little to do with the actual file system until you call one of its methods. In fact, you can create File instances of paths that don't even exist.