Download presentation
Presentation is loading. Please wait.
Published byTracey Burke Modified over 9 years ago
1
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning COMP 112 2015T1 #6
2
COMP 112 6: 2 Overview Reading and writing from files Paths Try {..} catch {..} for risky code. Scanners
3
COMP 112 6: 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 6: 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 This 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 Best to understand simple Example Code:
5
COMP 112 6: 5 Files Examples of common idioms: new Scanner(new File("README.TXT"))); new BufferedReader(new FileReader("README.TXT"))); new File("README.TXT") dose not mean that the file README.TXT is new (just created). It means that a new object in the java program is created linking to a potentially existing README.TXT file. new Scanner(new File("README.TXT"))); creates a new object that will scan (read and output tokens) from the README.TXT file.
6
COMP 112 6: 6 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 will crash the program!
7
COMP 112 6: 7 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
8
COMP 112 6: 8 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
9
COMP 112 6: 9 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")));
10
COMP 112 6: 10 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
11
COMP 112 6: 11 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*");
12
COMP 112 6: 12 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.
13
COMP 112 6: 13 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.