Chapter 20.2: File processing and Exception Handling
File Processing
Pseudocode
Program code
Scanner
API: https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
Scanner
和其他读入的一个不同点就是他是存在于java.util
包下面的 而其他大多数是在java.io
包下面。
有一个好处就是scanner.hasNext
可以用来检查文件是否读完
Declaration/construction
1 | Scanner sca1 = new Scanner(System.in);//从console输入 |
Methods:
A Scanner
breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next
methods.
简单来说 被空格、换行符所分割的 每一个部分 都算一个token
比如"1 2 3 Samuel"
中 1
,2
,3
,"Samuel"
都算token
. 1
可以用nextInt()
来读取, "Samuel"
可以用 next()
来读取。
hasNext()
- Returns true if this scanner has another token in its input.
next()
- Finds and returns the next complete token from this scanner.
nextLine()
- Advances this scanner past the current line and returns the input that was skipped.
nextInt()
- Scans the next token of the input as an
int
.
- Scans the next token of the input as an
⚠ 使用scanner中遇到的nosuchfileException
FileReader
API: https://docs.oracle.com/javase/8/docs/api/java/io/FileReader.html
通常在考试中FileReader都是作为创建BufferedReader的一个前置来使用。
Declaration/construction
1 | FileReader(File file);//can construct with a file |
BufferedReader
API: https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html
Buffered Reader 相对于 传统Reader的好处就是 当读入一个文件的时候 能够先把文件的内容存在Buffer(缓存)里面 然后每次从Buffer里面读取,这样比传统Reader直接从文件里读取更加高效
Declaration/construction
1 | File fi = new File(fileName); |
Methods(考试会用到的):
readLine()
: Reads a line of text. A line is considered to be terminated by any one of a line feed (‘\n’), a carriage return (‘\r’), or a carriage return followed immediately by a linefeed.
1 | public String readLine() |
RandomAccessFile
API: https://docs.oracle.com/javase/8/docs/api/java/io/RandomAccessFile.html
Instances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer
; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended. The file pointer can be read by the getFilePointer
method and set by the seek
method.
Data type | Size |
---|---|
byte |
1 byte |
short |
2 bytes |
int |
4 bytes |
long |
8 bytes |
float |
4 bytes |
double |
8 bytes |
boolean |
1 bit |
char |
2 bytes |
Declaration/construction
1 | RandomAccessFile(File file, String mode); |
mode
mode
本质上就是读写模式,像是pseudocode 中 OPENFILE <file Name>
FOR <File mode>
中的 <File mode>
一样。
"r"
open for read only."rw"
open for read and write
Methods(考试会用到的)
read()
- Reads a byte of data from this file.
readBoolean()
- Reads a
boolean
from this file.
- Reads a
readByte()
- Reads a signed eight-bit value from this file.
readChar()
- Reads a character from this file.
readDouble()
- Reads a
double
from this file.
- Reads a
readInt()
readUTF()
seek(long pos)
- Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
- Inputs the number, representing the position that you want the
file pointer
to be.
writeInt(int num)
writeChars(String str)
- Writes a string to the file as a sequence of characters.
writeDouble(String str)
- Writes a string to the file using modified UTF-8 encoding in a machine-independent manner.
Example code(from Kamana):
1 | import java.io.*; |
Useful resources
Exception Handling
All exceptions are
class
that are derived from theThrowable class
Exception hierarchy^1
1 | Exception |
Throwable
is a derived class of object
. Throwable
has two subcategories: Error
and Exception
Error
: severe problems occurred that cannot be solved
Exception
: problems that occur when the program is running. It can be captured by try...catch
RuntimeException
as well as its subclasses- Other Exceptions / Non-RuntimeException Exceptions
- Exceptions other than RuntimeException must be
try...catch
; else the compiler would report and halt Errors
andRuntimeException
as well as its derived classes are not forced to betry...catch
Capture the exception: try...catch
Single catch
1 | try{ |
Multiple catch
It is possible to have multiple catch, so that different program could be run when different exceptions are encountered.
Be aware that only one catch will be executed:
When the
try
program is run, it halts and jumps to thecatch
section when an exception occurThe compiler reads the catch one by another in sequence. If the exception coincides, the corresponding program is executed.
And then it ends
Hence, it is important to note that Exception1 should be the subclass of Exception2, and Exception2 should be the subclass of Exception3. Else it is impossible to execute the second and third catch
1 | try{ |
Finally
The statement that will be executed anyway no matter whether the Exception occur in the try processes
1 | try{ |