Class HighLite File
Many people might be familiar with the File class. You need a File object for many of Java’s IO operations. In this Class High Lite article I cover some of the File’s least known methods and fields.
Please note that in all cases be sure to read the Java documentation, as it is the best available source of information regarding the Java API. You can also always take a pick into the source code provided with the Sun JDK.
I often see “\\” in strings to separate directory and a file names. It is strongly suggested to not use operating dependent strings to separate directories and files. The backward character is a Windows specific way to separate a directory hierarchy, while Linux and Apple use a different character. To avoid using hard coded strings to concatenate a file path use the File’s separator field. The value of separator contains the correct file separator character for your operating system.
The listRoots method is a convenient way to to access all the available drives available in your system. This method might be helpful to enhance the default JFileChooser and have a drives drop down list. This method returns an array of File objects contain the root letter drives, your file system roots.
Some people procrastinate, but does their code procrastinate also? The deleteOnExit method waits until the JVM terminates to delete a file. Be sure to read the JavaDoc. According to the JavaDoc 1.4.1, “Deletion will be attempted only for normal termination of the virtual machine.” When your program exits, the file marked for deletion is removed, even if the program exits with an exception. As the JavaDoc states, the file will be deleted when the JVM exits normally, and an exception is normal termination of the JVM although not for your program.