1 / 11

AP Computer Science

Mr. Wortzman. AP Computer Science. File I/O. 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.

laurel
Download Presentation

AP Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mr. Wortzman AP Computer Science

  2. File I/O • 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. The File class • 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. File I/O • 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. Checked Exceptions • 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 throws clause • 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. File I/O • 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 (inti = 0; i < words.length; i++) { output.println(words[i]); }

  8. File I/O Notes • 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. Combining Lines and Tokens • 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. Combining Lines and Tokens • 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. File I/O • 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

More Related