Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning.

Similar presentations


Presentation on theme: "David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning."— Presentation transcript:

1 David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning COMP 112 2014T1 #11

2 COMP 112 11: 2 Overview Reading and writing from files Paths Try {..} catch {..} for risky code. Scanners

3 COMP 112 11: 3 Introduction Java file handling uses many Classes including: 1.File FileReader FileWriter 2.Path Look at the API to find specialised file handling. Scanners transform a string into a sequence of tokens. Frequently the contents of asci files are read using a scanner. Scanners implement the interface Iterator

4 COMP 112 11: 4 Files Java will find files in the directory from which the program is run just by using the file name. Java IO design is based on the Decorator pattern Thus can lead to unhelpfully complex libraries! Beware java NIO (nonblocking) exists as well My advice is keep it simple and understand the basics 1. File internal representation of a file 2. FileReader for reading one byte at a time 3. BufferedReader for reading one line at a time 4. Scanner for reading one word (token) at a time Decorator code from our example is a common idiom: new Scanner(new File("README.TXT"))); new BufferedReader(new FileReader("README.TXT")));

5 COMP 112 11: 5 Risky code Any thing that is risk (opening a file that might not exist) should be performed in a try block try { …} and you need to catch these errors catch { …} and process them. Even if only by printing out an error message. Uncaught errors crash the program!

6 COMP 112 11: 6 Example From “ README.TXT ” we build a File then a Scanner. Build a Scanner Failing can throw exceptions File opening can fail Catch IOexceptions from the try block Open a file

7 COMP 112 11: 7 Paths and Files Files are held in a rooted directory tree structure. A Complete Path has the names from the root down the tree to the file. /home/users/dstr/Comp112/Lectures/foo.ppt A Relative Path is a Path from the current location to a file. If the program is run in my home directory /home/users/dstr the files relative path is Comp112/Lectures/foo.ppt Note absolute paths start with a / but relative paths do not. Unix and Microsoft computers use paths with different separators. Unix uses / but Microsoft uses \. Java can use File.separator and the JVM will automatically adjust depending on the operating system

8 COMP 112 11: 8 File Choosing When you don not know the name of the file but want to browse the directories use, s = new Scanner(new File(UIFileChooser.open())); in place of, s =new Scanner(new File("README.TXT")));

9 COMP 112 11: 9 Scanner Class The Scanner class provides all the text input methods from the UI class. But can take input from Files s = new Scanner(new File(“README.TXT”)); The following methods you should recognize hasNext, hasNextInt, hasNextBoolean, hasNextDouble next, nextInt, nextBoolean, nextDouble, nextLine Scanner can take as input: 1. File 2. Path 3. String

10 COMP 112 11: 10 Delimiter Scanner default delimiter is white space that is: the sequence of token the scanner reads are separated by white space. To change the delimitor use s.useDelimiter(",\\s*");

11 COMP 112 11: 11 Exercises: Read a file where each line can have different number of tokens (words). Output the sequence of tokens and special token to mark where the lines end. Read a file with n integers on each of m lines and output an n*m array of integers.

12 COMP 112 11: 12 Summary Classes: File, Path, Scanner, UIFileChooser 1.Look at the API 2.Look at code examples Decorator pattern – wrapping one class around another - is a common idiom but has known problems


Download ppt "David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning."

Similar presentations


Ads by Google