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?
4 Answers
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.
2 Comments
java.nio.channels.FileLock class.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