File Input and Output
File Input and Output
File I/O
The action of saving, or writing, data to a file is called file output. The action of reading data from a file is called file input. Both of these actions involve interacting with the operating system and its file structure
Opening a file
The simplest syntax for opening a file is:
File aFile = new File (filename); The String argument designates the name of the file (as seen by the operating system) This syntax assumes that the argument is the name of a file (not a folder), and that the file is located in the same folder as the programs code file
Opening a file
Slightly more complicated syntax is necessary if the file to be opened is in a different folder:
File aFile = new File (path, file); The first argument specifies the disk and file system hierarchy that contains the file For example, if the file is in directory G:\shared\cate\cs1, you would write:
File aFile = new File (g:/shared/cate/cs1, file); Note the use of the forward slash; you can use either this format or: File aFile = new File (g:\\shared\\cate\\cs1, file);
Opening a file
A File object may also be associated to a directory. As far as Java is concerned, a directory is just a special kind of file The code on the next slide illustrates the use of a File object to open a directory, then list the names of all the files in the directory
Example
File directory = new File (C:/JavaPrograms/Ch12); String filename[] = directory.list();
// Returns an array of strings naming the files and // directories in the directory denoted by this pathname
File methods
The previous slide illustrated the use of the list() method, which returns an array of Strings containing the names of files within a directory Several other useful methods of the File class are listed in the Java API; we will examine a few of these
File methods
We can check if a File object is associated correctly to an existing file by calling its exists method:
if (inFile.exists()) { // inFile is associated correctly to an existing file } else { // inFile is not associated to any existing file }
It is particularly useful to check input files to see if they actually exist before we try to read from them
File methods
To determine if a File object is associated to a file or directory, we call its boolean method isFile. A similar method, isDirectory, returns true if the File object is associated with a folder instead of an ordinary file.
JFileChooser Example
The next couple of slides display a program that uses JFileChooser objects to display a list of files, then allow the user to choose which file should be open for output Note the use of File and JFileChooser methods in the code
import java.io.*; import javax.swing.*; public class TestJFileChooser { public static void main (String[] args) { JFileChooser chooser; File file, directory; int status; chooser = new JFileChooser( ); status = chooser.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION) { file = chooser.getSelectedFile(); directory = chooser.getCurrentDirectory(); System.out.println("Directory: " + directory.getName()); System.out.println("File selected to open: " + file.getName()); System.out.println("Full path name: " + file.getAbsolutePath()); } else { JOptionPane.showMessageDialog(null, "Open File dialog canceled"); }
System.out.println("\n\n"); status = chooser.showSaveDialog(null); if (status == JFileChooser.APPROVE_OPTION) { file = chooser.getSelectedFile(); directory = chooser.getCurrentDirectory(); System.out.println("Directory: " + directory.getName()); System.out.println("File selected for saving data: " + file.getName()); System.out.println("Full path name: " + file.getAbsolutePath()); } else { JOptionPane.showMessageDialog(null, "Save File dialog canceled"); } } }
Finally, we convert the String to a primitive data type as necessary (using wrapper classes as weve done before).
Different types of objects may be saved to a single file. We can also mix objects and primitive data type values in the same file.