简体   繁体   中英

Using java.io.File class

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?

Doing

File f = new File("C:\\test.txt");

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.

IF you have written,

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

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,

f.createNewFile();

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM