Sequential Byte Stream File IO Using Input- OutputStreams


Using The File Class

The File class represents the name of a file or path. It is used to manipulate file metadata information such as the filename, directory listing, file length, whether the filename is a directory or a file, whether the file is readable, writable, or both, etc.

You do not need to use a File object to create a file for I/O operations because all the file terminal class constructors can take a String as well as a File as a argument. However, at times you will need the functionality provided by the File class so it’s important that you understand its operation.

Example 18.1 gives a short program that creates a File object and calls a few of its methods.

Example 18.1: FileClassTesterApp.java

image from book
 1     import java.io.*; 2 3     public class FileClassTesterApp { 4        public static void main(String[] args){ 5           File file = null; 6             try{ 7               file = new File(args[0]); 8           System.out.println(file.getName()); 9           System.out.println(file.getPath()); 10          System.out.println(file.getAbsolutePath()); 11          System.out.println(file.getCanonicalPath()); 12          System.out.println(file.isDirectory()); 13 14          }catch(Exception ignored){ } 15       } 16    }
image from book

Referring to example 18.1 — this program takes the text string entered on the command line and creates a File object having that name. The File reference is declared on line 5 and the File object is created on line 7. Various methods are called on the File object on lines 8 through 12.

It’s important to note that although a File object is created, the actual file is not created. This is demonstrated by running the example. The results of running this program are shown in figure 18-2.

image from book
Figure 18-2: Results of Running Example 18.1

Referring to figure 18-2 — notice that before the program was executed an ls (list directory) was performed which showed there were two files in the directory: image from book FileClassTesterApp.class and image from book FileClassTesterApp.java. During program execution the File object is created and a few of its methods called. When the program completes another ls is performed and yields the same two classes in the directory.

You can create a file with a File object by calling its createNewFile() method but creating a file in this manner results in an empty file.

Example 18.2 shows how you can use the File class in conjunction with the JFileChooser swing component to open or create a new File object.

Example 18.2: JFileChooserTestApp.java

image from book
 1     import javax.swing.*; 2     import java.io.*; 3 4     public class JFileChooserTestApp { 5       public static void main(String[] args){ 6         int   chooser_action; 7         File  file; 8         JFileChooser file_chooser = new JFileChooser(); 9         chooser_action = file_chooser.showOpenDialog(new JFrame()); 10        if(chooser_action == JFileChooser.APPROVE_OPTION){ 11           file = file_chooser.getSelectedFile(); 12           System.out.println(file.getName()); 13           System.out.println(file.getPath()); 14           System.out.println(file.length()); 15           System.exit(0); 16         }else{ 17           System.exit(0); 18              } 19       } 20    }
image from book

Referring to example 18.2 — a JFileChooser object is created on line 8 and is used on lines 10 and 11 to choose an existing file or to create a new file. When the file is selected the file reference is initialized on line 11 by a call to the JFileChooser getSelectedFile() method. Several methods are called via the file reference on lines 12 through 14 and then the program exits. Figure 18-3 shows the results of running this program.

image from book
Figure 18-3: Results of Running Example 18.2

Referring to figure 18-3 — upon application execution the JFileChooser dialog opens and allows a file to be selected. In this example the source file image from book JFileChooserTestApp.java is selected and the Open button is clicked. This results in the file’s information being written to the console.

Quick Review

The File class is used to manipulate file metadata such as a file’s name, length, and path. The creation of a File object does not result in an actual file being created on disk, however, you can create an empty file by calling the createNewFile() method.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net