Java核心类库(下)
参加拉勾教育大数据训练营课程笔记
Java异常机制
Throwable类
- java.lang.Object
- java.lang.Throwable
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
Error, Exception
是所有Error,Exception的父类(超类)。
Error是JVM遇到的无法解决的严重错误Exception是程序错误,或者偶然外在因素导致的错误,可以通过修改代码解决
Exception分类
RuntimeException,运行时异常,也称非检测性异常IOException和其他Exception,也叫检测性异常,是指 编译阶段能被检测出来
异常处理
多个异常捕获时,小的放前,大的放后
try {}
catch (ExceptionSmall e) {}
catch (ExceptionBig e) {}
finally {}
finally考点
public class Test {
private static int t(int a, int b) {
try {
return a / b;
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
return 1;
} finally {
return 2;
}
}
public static void main(String[] args) {
int a = 1;
int b = 0;
System.out.println(t(a, b));
}
}
输出结果:
/ by zero
2
catch中的return得不到执行,而是进入了finally,会绕过return,continue,break。下面的代码块,在catch执行return前,finally代码块得到执行,然后回到catch,继续执行return 1返回1。
package phase01.module4.code;
public class Test {
private static int t(int a, int b) {
try {
return a / b;
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
return 1;
} finally {
System.out.println("finally");
}
}
public static void main(String[] args) {
int a = 1;
int b = 0;
System.out.println(t(a, b));
}
}
运行结果:
/ by zero
finally
1
Notes
The
finallyblock always executes when thetryblock exits. This ensures that thefinallyblock is executed even if an unexpected exception occurs. Butfinallyis useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by areturn,continue, orbreak. Putting cleanup code in afinallyblock is always a good practice, even when no exceptions are anticipated.
方法重写回顾:
-
参数相同
-
访问权限可以不变或更大
-
返回类型相同或父类
-
方法重写不能抛出更大的异常,或平级的不同异常,可以抛出相同或更小的异常,可以不抛出异常
抛出或捕获:
-
父类没有抛出异常时,重写方法时只能捕获处理
-
多层调用时,最后一次调用捕获处理异常
自定义异常类
- 继承
Exception或其子类。 - 提供两个构造方法:无参,字符串参数
public class AgeException extends Exception {
public AgeException() {
super();
}
public AgeException(String message) {
supper(message);
}
}
File类
- java.lang.Object
- java.io.File
All Implemented Interfaces:
Serializable, Comparable<File>
public class File
extends Object
implements Serializable, Comparable<File>
字段
| Modifier and Type | Field | Description |
|---|---|---|
static String | pathSeparator | The system-dependent path-separator character, represented as a string for convenience. |
static char | pathSeparatorChar | The system-dependent path-separator character. |
static String | separator | The system-dependent default name-separator character, represented as a string for convenience. |
static char | separatorChar | The system-dependent default name-separator character. |
构造方法
| Constructor | Description |
|---|---|
File(String pathname) | Creates a new File instance by converting the given pathname string into an abstract pathname. |
File(File parent, String child) | Creates a new File instance from a parent abstract pathname and a child pathname string. |
File(String parent, String child) | Creates a new File instance from a parent pathname string and a child pathname string. |
File(URI uri) | Creates a new File instance by converting the given file: URI into an abstract pathname. |
常用方法
| Modifier and Type | Method | Description |
|---|---|---|
boolean | canExecute() | Tests whether the application can execute the file denoted by this abstract pathname. |
boolean | canRead() | Tests whether the application can read the file denoted by this abstract pathname. |
boolean | canWrite() | Tests whether the application can modify the file denoted by this abstract pathname. |
int | compareTo(File pathname) | Compares two abstract pathnames lexicographically. |
boolean | createNewFile() | Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. |
boolean | delete() | Deletes the file or directory denoted by this abstract pathname. |
void | deleteOnExit() | Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. |
boolean | equals(Object obj) | Tests this abstract pathname for equality with the given object. |
boolean | exists() | Tests whether the file or directory denoted by this abstract pathname exists. |
File | getAbsoluteFile() | Returns the absolute form of this abstract pathname. |
String | getAbsolutePath() | Returns the absolute pathname string of this abstract pathname. |
String | getName() | Returns the name of the file or directory denoted by this abstract pathname. |
String | getParent() | Returns the pathname string of this abstract pathname’s parent, or null if this pathname does not name a parent directory. |
File | getParentFile() | Returns the abstract pathname of this abstract pathname’s parent, or null if this pathname does not name a parent directory. |
boolean | isDirectory() | Tests whether the file denoted by this abstract pathname is a directory. |
boolean | isFile() | Tests whether the file denoted by this abstract pathname is a normal file. |
long | lastModified() | Returns the time that the file denoted by this abstract pathname was last modified. |
long | length() | Returns the length of the file denoted by this abstract pathname. |
File[] | listFiles() | Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. |
boolean | mkdir() | Creates the directory named by this abstract pathname. |
boolean | mkdirs() | Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. |
boolean | setExecutable(boolean executable) | A convenience method to set the owner’s execute permission for this abstract pathname. |
boolean | setExecutable(boolean executable, boolean ownerOnly) | Sets the owner’s or everybody’s execute permission for this abstract pathname. |
boolean | setLastModified(long time) | Sets the last-modified time of the file or directory named by this abstract pathname. |
boolean | setReadable(boolean readable) | A convenience method to set the owner’s read permission for this abstract pathname. |
boolean | setReadable(boolean readable, boolean ownerOnly) | Sets the owner’s or everybody’s read permission for this abstract pathname. |
boolean | setReadOnly() | Marks the file or directory named by this abstract pathname so that only read operations are allowed. |
boolean | setWritable(boolean writable) | A convenience method to set the owner’s write permission for this abstract pathname. |
boolean | setWritable(boolean writable, boolean ownerOnly) | Sets the owner’s or everybody’s write permission for this abstract pathname. |
IO流
按读写数据类型分:
- 字节流,可以读写任意类型的文件
- 字符流(2个字节),只能读取文本文件
以运行程序视角,按方向分为:
- 输入流
- 输出流
按流的角色分为:
- 节点流,直接与文件(输入输出流)直接关联
- 处理流,与其他流关联
体系结构表
| 分类 | 字节输入流 | 字节输出流 | 字符输入流 | 字符输出流 |
|---|---|---|---|---|
| 抽象基类 | InputStream | OutputStream | Reader | Writer |
| 访问文件 | FileInputStream | FileOutputStream | FileReader | FileWriter |
| 访问数组 | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
| 访问管道 | PipedInputStream | PipedOutputStream | PipedReader | PipedWriter |
| 访问字符串 | - | - | StringReader | StringWriter |
| 缓冲流 | BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
| 转换流 | - | - | InputStreamReader | OutputStreamWriter |
| 对象流 | ObjectInputStream | ObjectOutputStream | - | - |
| FilterInputStream | FilterOutputStream | FilterReader | FilterWriter | |
| 打印流 | - | PrintStream | - | PrintWriter |
| 推回输入流 | PushbackInputStream | - | PushbackReader | - |
| 特殊流 | DataInputStream | DataOutputStream | - | - |
课程掌握:

<<---------------- 未完待续 ---------------- >>

这篇博客深入探讨了Java异常处理机制,包括Throwable类、Error和Exception的区别,以及异常捕获和finally块的执行规则。此外,还详细介绍了File类的使用,如创建、删除文件,以及文件的读写权限设置等。最后提到了IO流的基本概念和分类。
1484





