Java Reference
In-Depth Information
File dummyFile = new File("dummy.txt");
Note that a file named dummy.txt does not have to exist to create a File object using this statement. The
dummyFile object represents an abstract pathname, which may or may not point to a real file in a file-system.
The File class has several methods to work with files and directories. Using a File object, you can create a new
file, delete an existing file, rename a file, change permissions on a file, and so on. You will see all these operations on a
file in action in subsequent sections.
the File class contains two methods, isFile() and isDirectory() . use these methods to know whether a
File object represents a file or a directory.
Tip
Knowing the Current Working Directory
The concept of the current working directory is related to operating systems, not the Java programming language or
Java I/O. When a process starts, it uses the current working directory to resolve the relative paths of files. When you
run a Java program, the JVM runs as a process, and therefore it has a current working directory. The value for the
current working directory for a JVM is set depending on how you run the java command.
You can get the current working directory for the JVM by reading the user.dir system property as follows:
String workingDir = System.getProperty("user.dir");
At this point, you may be tempted to use the System.setProperty() method to change the current working
directory for the JVM in a running Java program. The following snippet of code will not generate any errors; it will not
change the current working directory either:
System.setProperty("user.dir", "C:\\kishori");
After you try to set the current working directory in your Java program, the System.getProperty("user.dir")
will return the new value. However, to resolve the relative file paths, the JVM will continue to use the current working
directory that was set when the JVM was started, not the one changed using the System.setProperty() method.
Java designers found it too complex to allow changing the current working directory for the JVM in the middle of
a running Java program. For example, if it were allowed, the same relative pathname would resolve to different absolute
paths at different times in the same running JVM, giving rise to inconsistent behavior of the program.
Tip
You can also specify the current working directory for the JVM as the user.dir property value as a JVM option. To
specify C:\test as the user.dir system property value on Windows, you run your program like so:
java -Duser.dir=C:\test your-java-class
 
 
Search WWH ::




Custom Search