12slide Accessible
12slide Accessible
Data Structures
Thirteenth Edition
Chapter 12
Exception Handling and
Text IO
Quotient
QuotientWithIf
With a method
QuotientWithMethod
ClassNotFoundException
ArithmeticException
IOException
Exception NullPointerException
RuntimeException
IndexOutOfBoundsException
Many more classes
Object Throwable IllegalArgumentException
Error VirtualMachineError
Call Stack
method3
method2 method2
...
}
CircleWithException
TestCircleWithException
try {
System.out.println(refVar.toString());
System.out.println("refVar is null");
}
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
When to Use Exceptions (2 of 2)
is better to be replaced by
if (refVar != null)
System.out.println(refVar.toString());
else
System.out.println("refVar is null");
InvalidRadiusException
CircleWithRadiusException
TestCircleWithRadiusException
assert assertion; or
assert assertion : detailMessage;
switch (month) {
case 1: ... ; break;
case 2: ... ; break;
...
case 12: ... ; break;
default: assert false : "Invalid month: "
+ month
}
TestFileClass
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Text I/O
A File object encapsulates the properties of a file or a path, but does not contain
the methods for reading/writing data from/to a file. In order to perform I/O, you
need to create objects using appropriate Java I/O classes. The objects contain
the methods for reading/writing data from/to a file. This section introduces how
to read/write strings and numeric values from/to a text file using the Scanner
and PrintWriter classes.
WriteDataWithAutoClose
java.util.Scanner
+Scanner(source: File) Creates a Scanner object to read data from the specified file.
+Scanner(source: String) Creates a Scanner object to read data from the specified string.
+close() Closes this scanner.
+hasNext(): boolean Returns true if this scanner has another token in its input.
+next(): String Returns next token as a string.
+nextByte(): byte Returns next token as a byte.
+nextShort(): short Returns next token as a short.
+nextInt(): int Returns next token as an int.
+nextLong(): long Returns next token as a long.
+nextFloat(): float Returns next token as a float.
+nextDouble(): double Returns next token as a double.
+useDelimiter(pattern: String): Sets this scanner’s delimiting pattern.
Scanner
ReadData
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Problem: Replacing Text
Write a class named ReplaceText that replaces a string in a text
file with a new string. The filename and strings are passed as
command-line arguments as follows:
java ReplaceText sourceFile targetFile oldString newString
For example, invoking
java ReplaceText FormatString.java t.txt StringBuilder
StringBuffer
replaces all the occurrences of StringBuilder by StringBuffer in
FormatString.java and saves the new file in t.txt.
ReplaceText
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Reading Data From the Web (1 of 2)
Just like you can read data from a file on your computer,
you can read data from a file on the Web.
ReadFileFromURL
WebCrawler
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Copyright