Input and Output Using Text Files and Exception Handling. Text File I/O and Exception Handling. File Input and Output. Sometimes, we need to save output to use later Retyping test data each time one tests a program is tedious for the user/tester
Input and Output Using Text Files and Exception Handling
Text File I/O and Exception Handling
This is an example of a “|-delimited” file.
The individual fields are separated by some character that is not found anywhere in the data. In this case, that is the “|” character.
Screen snapshot taken from a text editor such as Notepad++
JFileChooser ( )
JFileChooser (String path)
JFileChooser fileChooser = new JFileChooser ( );
int status = fileChooser.showOpenDialog (null);
if (status == JFileChooser.APPROVE_OPTION)
File selectedFile = fileChooser.getSelectedFile ( );
Scanner input = new Scanner(selectedFile);
// Now we can input from this file in standard way
Filter – show only .txt files
In project but in different folder than .src
This may come from the JFileChooser results
Refer to the file by this name in the rest of the program
PrintWriter outputFile = new PrintWriter("StudentData.txt");
Pass thename of the filethat you wish to open as a stringargumentto thePrintWriterconstructor.
If the output filealreadyexists, it will beerasedandreplacedwith anewfile.
Create and Open the PrintWriter file object
PrintWriter outFile = new PrintWriter (“Data/Names.txt");
outFile.close ( );
Names.txt is in subfolder named Data – this could be the results from using JFileChooser
Close the file object
Write data to the file
public static void main (String[ ] args)throws IOException
Thetrueparameter indicates this data should be added to the end of the existing file rather than replacing the data already there.
PrintWriter outFile = new PrintWriter (“C:\\nature.txt");
PrintWriter outFile = new
Creates an output file named ContactList.txt in the ContactData subfolder and fills it with information (Contact objects) from addressBook. The fields in a Contact are delimited by pipe characters (“|”).
Pass the name of the file as an argument to the File class constructor. The name may have come from the results of JFileChooser
File myFile = new File("Customers.txt");
Scanner inputFile = new Scanner(myFile);
Pass the File object as an argument to the Scanner class constructor.
Scanner keyboard = new Scanner (System.in);
System.out.print ("Enter the filename: ");
String fileName = keyboard.nextLine ( );
File file = new File (fileName);
Scanner inputFile = new Scanner (file);
throws clause example
We could do this line in a loop to input more than one line from file
// Open the file
File file = new File(filename);
Scanner inputFile = new Scanner(file);
// Read until the end of the file is reached
while (inputFile.hasNext( ))
String str = inputFile.nextLine( );
inputFile.close();// close the file when done
Try to open input file – exception thrown if unsuccessful
Terminate the pgm
File is in subfolder named ContactData
While there is more data
The \\ are needed because the | is must be “escaped” in the split method
Fields separated by “|”
The fields are separated by the pipe character (“|”) to make it easy to determine where one field ends and the next begins