Page 668 - Introduction to Programming with Java: A Problem Solving Approach
P. 668
634 Chapter 15 Files
In Figure 15.16, look for each of the following operations:
• AJFileChooserconstructorcall.
• A call to JOptionPane’s showConfirmDialog method, and use of the value returned.
• AcalltoJFileChooser’ssetFileSelectionModemethod.
• AcalltoJFileChooser’sshowOpenDialogmethod,anduseofthevaluereturned.
• AcalltoJFileChooser’sgetSelectedFilemethod,anduseofthevaluereturned.
• AcalltoJOptionPane’sshowMessageDialogmethod.
After calling getSelectedFile, the program needs to determine the type of the user’s selection—file or directory. The isFile and isDirectory calls take care of that. Within the directory-processing code, note how fileDir, a File object, calls listFiles. The listFiles method returns the files and directories that are in the fileDir directory. The returned files and directories are stored as File ob- jects in an array named files. After filling the files array, the program loops through each of its File elements. For each element, it prints filename and file size by calling getName and length, respectively.
In the FileSizesGUI program, note the String.format method calls. The String.format method works the same as the printf method except that instead of printing a formatted value, it returns a formatted value. We use the String.format method calls in an attempt to display values with uniform widths. Specifically, we want to display the filenames and file sizes with uniform widths, so that the file-size values display in an aligned fashion. But the bottom dialog box in Figure 15.15 shows that the file-size val- ues are not aligned. The problem is that with GUI output, different characters print with different widths. For example, you can see that the “HTML” in HTMLGenerator.class is wider than the “File” in FileSizesGUI.java. Thus, the HTMLGenerator.class line is longer. Having dif-
ferent characters print with difAfeprenat wgidoths iPs DstyFlish mEonst hofathne ctime,rbut in our
FileSizesGUI program, it’s annoying. To fix this problem, you could embed a JTextArea
component into the JOptionPane dialog box and set the JTextArea component’s font
to a monospaced font (with a monospaced font, every character prints with the same width).
You’ll learn about the JTextArea component in Chapter 17.
Summary
• You can find most of the file-transfer classes you’ll need in the java.io package.
• To output text to a new file, open the file by instantiating a PrintWriter object with a String file- name as the constructor argument. Write to the file by calling PrintWriter’s println, print, or
printf method, and close the file by calling PrintWriter’s close method.
• To append text to an existing file, open the file by instantiating a PrintWriter object with an anon- ymous FileOutputStream object as the constructor argument. Use filename and true for the
FileOutputStream constructor arguments.
• To input text from a file, open the file by instantiating a Scanner object with an anonymous
FileReader object as the constructor argument. Use the filename for the FileReader construc- tor argument. Read from the file by calling one of Scanner’s methods, and close the file by using Scanner’s close method.
• You can use text file I/O to translate plain-text information into HTML format for a Web page.
• The data in a text file appears as a sequence of bytes, where each byte corresponds to one character, and lines are delimited by \r\n or \n symbols. You can use a text editor or word processor with Plain Text
Use monospaced font to align text.