- Line-Based Processing
- Advanced File Processing
- Case Study: Zip Code Lookup
-
Details of Token-based Processing
-
Structure of Files and Consuming input
- computer consider the 2D file as 1 line sequence of characters
- with special characters like \n or \\
-
InputMismatchException
- wrong type of token
-
Input Cursor
- When scanner obj first constructed: at the beginning
- move forward
- no support for backward
-
Consuming input
- process
- it simply skips past any leading whitespace chars until next token
- Scanner keeps tracking the position of the input cursor. Even a method has passed, it still remembers.
- numbers.dat ---- Scanner obj
-
token
- number, character...
-
paths and directions
- File path
- file
-
directory/folder
- current directory(a.k.a Working directory)
-
call a file
-
fully qualified
- absolute file path
- C:/data/numbers.dat
- forward slash /
-
relative path
- data/numbers.dat
- waiting for users input file name
-
File-Reading Basics
- Data, Data Everywhere
-
Files and file objects
- file
-
file extension
- data kind
- format
- access file=>construct internal object
-
Reading a file from Scanner
- File f = new File("hamlet.txt");
- construct internal obj to access file data in computer
-
import java.io.*;
- file i/o
- Scanner input = new Scanner(new File("hamlet.txt"))
-
import java.util.*;
- scanner
-
FileNotFoundException
- computer concerns it might not be found
- Checked Exception
- public static void main(...) throws FileNotFoundException{...}
-
NoSuchElementException
- end of file
- Scanner need to have a type declaration