Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use.

Similar presentations


Presentation on theme: "Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use."— Presentation transcript:

1 Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use a file it must be Opened – Prepares the file for access by creating a connection. Data written to, or read from the file. Closed – Terminates the connection. Earlier, when reading user input, we used the Scanner: Scanner kbd = new Scanner(System.in);  open statement // read from the user kbd.close();  close statement

2 Reading from a file We can use the Scanner class to create a connection to a file. Same basic procedure as before, it just takes a few more steps! See code listing 4-19 in your book. In class example will be similar. Because the file we want to read, or write, may not exist, or may not be accessible, our code can have an error Java will throw an exception when it has an error that it can’t handle.

3 1. Another import statement. This one is for I/O.
2. New clause on main method. This says that the method can ”throw” an IOException. This block prompts the user for the file name and saves the result. 3. New! Create a File object. 4. Create a Scanner object that reads from the file! 5. When hasNext() returns false, the full file has been read. 5. Read the next line from the file and save into aLine 6. Close the file

4 Reading from a file - Summary
Add import java.io.*; Add throws IOException to main statement Create a File object. Optionally verify that the file exists. Create a Scanner object using the File object Using a while loop: Verify data available via a call to Scanners’ hasNext() Read data Close

5 Reading from a file - Scanner
Scanner can be used to read whole and real numbers as well. One approach is to read a String and convert String tmp = infile.nextLine(); int val = Integer.parseInt(tmp); The other is to read an int while (hasNextInt()) { int val = infile.nextInt(); } See your book Ex 4-19 and 4-20. Ex. ScannerReadNumEx

6 Reading from a file – Buffered Reader
You can use a BufferedReader instead of the Scanner to read from a file. Alternative Mechanism – Most programming languages have multiple ways to do the same thing. More efficient than Scanner Slightly different methods than with Scanner.

7 Writing to a file We will use the PrintWriter class to write to a file. // To create or overwrite PrintWriter pw = new PrintWriter(“LogFile”); // To create use a FileWriter and set mode to false FileWriter fw = new FileWriter(“LogFile”, false); PrintWriter pw = new PrintWriter(fw); // Can put in 1 line … same behavior PrintWriter pw = new PrintWriter(new FileWriter(“LogFile”, false)); WARNING! Destroys the current content of LogFile. true means append false means create (& destroy if exists)

8 Appending to a file We will use the PrintWriter class to write to a file. // To append must use a FileWriter and set mode to true FileWriter fw = new FileWriter(“LogFile”, true); PrintWriter pw = new PrintWriter(fw); // Can put in 1 line … same behavior PrintWriter pw = new PrintWriter(new FileWriter(“LogFile”, true)); true means append false means create (& destroy if exists)


Download ppt "Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use."

Similar presentations


Ads by Google