Java class File has 4 constructors:
Creates a new File instance from a parent abstract pathname and a child pathname string.File(File parent, String child)
Creates a new File instance by converting the given pathname string into an abstract pathname.File(String pathname)
Creates a new File instance from a parent pathname string and a child pathname 开发者_C百科string.File(String parent, String child)
File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.
When I do:
File f=new File("myfile.txt");
Does a physical file on disk get created? Or does JVM make call to OS or does this only create an object inside JVM?
No, creating a new File
object does not create a file on the file system. In particular, you can create File
objects which refer to paths (and even drives on Windows) which don't exist.
The constructors do ask the underlying file system representation to perform some sort of normalization operations if possible, but this doesn't require the file to be present. As an example of the normalization, consider this code running on Windows:
File f = new File("c:\\a/b\\c/d.txt");
System.out.println(f);
This prints
c:\a\b\c\d.txt
showing that the forward slashes have been normalized to backslashes - but the a, b, and c directories don't actually exist. I believe the normalization is more to do with the operating system naming scheme rather than any actual resources - I don't believe it even looks on disk to see if the file exists.
精彩评论