Download presentation
Presentation is loading. Please wait.
Published byRay Appley Modified over 10 years ago
1
Mr. Wortzman
2
So far, we have gotten all our input and written all our output to the console In reality, this is somewhat uncommon Instead, we often use files for input and output This has the advantage of requiring much less user interaction
3
Most of this is done in Java through the File class File f = new File("myFile.txt"); This class has some useful methods: exists() - returns true if the named file exists getName() - returns the file's name renameTo() - changes the name of the file delete() - delete's the file from the disk See the Java API for more
4
None of these methods can work with the contents of the file however For that, we need to use either a Scanner (for input) or a PrintStream (for output) Scanner input = new Scanner(new File("input.txt")); PrintStream output = new PrintStream(new File("output.txt"));
5
What happens when you try to compile with the code on the previous slide? Certain types of errors must be dealt with somehow in the program These are considered dangerous, so Java wants to make sure you know they might occur Many types of file errors are considered checked exceptions
6
The easiest way to "deal with" these errors is to use a throws clause public static void readInput() throws FileNotFoundException {... } This tells Java "I know something bad might happen, and I accept the consequences if so" Any method that calls a method that throws must also throw
7
Once we've hooked up the file, we can use Scanner and PrintStream as usual Scanner input = new Scanner(new File("input.txt")); while (input.hasNext()) { System.out.print(input.next() + " "); } String[] words = {"cat", "dog", "bird", "hedgehog"}; PrintStream output = new PrintStream("output.txt"); for (int i = 0; i < words.length; i++) { output.println(words[i]); }
8
Important notes: Opening an existing file for output will overwrite the file ▪ You can use the exists() method of File to check for this Never open the same file for input and output at the same time-- everything will get erased You can walk off the end of an input file if you're not careful-- use the has methods to look before you leap
9
Files are typically written such that each line is a separate entity Therefore, we usually read files one line at a time while(file.hasNextLine()) {... } But each line might contain multiple tokens/values
10
Remember that we can make a Scanner from a String, so we can do something like: Scanner file = new Scanner(new File("myFile.txt")); while (file.hasNextLine()) { Scanner tokens = new Scanner(file.nextLine()); while (tokens.hasNext()) {... }
11
Exercise 1: Write a Java program to prompt the user for an input and output file, and copy the contents of the input file to the output file. Exercise 2: Write a Java program to read a file (specified by the user) containing a list of names and scores, and print out each person's total. ▪ Wortzman 5 7 12 8 ▪ Hawker 3 8 19 ▪ K 10 4 7 Should print ▪ Wortzman 32 ▪ Hawker 30 ▪ K 21
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.