Java

Io流

1. 字节流(以字节为单位) #

// 基类: InputStream 和 OutputStream

// 文件操作
FileInputStream fis = new FileInputStream("input.txt");
FileOutputStream fos = new FileOutputStream("output.txt");

// 示例:复制文件
try (FileInputStream fis = new FileInputStream("source.txt");
     FileOutputStream fos = new FileOutputStream("target.txt")) {
    byte[] buffer = new byte[1024];
    int len;
    while ((len = fis.read(buffer)) != -1) {
        fos.write(buffer, 0, len);
    }
}

// 缓冲流:提高读写效率
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file.txt"));
     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.txt"))) {
    bis.transferTo(bos); // Java 9+
}

// 数据流:读写基本数据类型
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.bin"))) {
    dos.writeInt(100);
    dos.writeUTF("Hello");
    dos.writeDouble(3.14);
}

// 对象流:序列化和反序列化
class User implements Serializable {
    private String name;
    private int age;
}

// 写入对象
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.obj"))) {
    User user = new User("张三", 25);
    oos.writeObject(user);
}

// 读取对象
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.obj"))) {
    User user = (User) ois.readObject();
}

2. 字符流(以字符为单位) #

// 基类: Reader 和 Writer

// 文件读写
try (FileReader fr = new FileReader("input.txt");
     FileWriter fw = new FileWriter("output.txt")) {
    char[] buffer = new char[1024];
    int len;
    while ((len = fr.read(buffer)) != -1) {
        fw.write(buffer, 0, len);
    }
}

// 缓冲字符流
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}

// 字符串读写
StringReader sr = new StringReader("Hello World");
StringWriter sw = new StringWriter();

3. 转换流(字节流转字符流) #

// InputStreamReader:字节流转换为字符流
try (InputStreamReader isr = new InputStreamReader(new FileInputStream("file.txt"), "UTF-8")) {
    char[] buffer = new char[1024];
    int len;
    while ((len = isr.read(buffer)) != -1) {
        System.out.print(new String(buffer, 0, len));
    }
}

// OutputStreamWriter:字符流转换为字节流
try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("file.txt"), "UTF-8")) {
    osw.write("你好,世界");
}

4. 实际应用场景 #

文件复制:

...

异常

1. 异常概述 #

异常是程序执行过程中出现的意外事件,会中断程序的正常流程。Java使用异常处理机制来处理运行时错误,使得程序更加健壮。

2. 异常层次结构 #

Java的所有异常都是java.lang.Throwable类的子类,主要分为两大类:

  1. Error:表示严重的问题,通常是不可恢复的系统级错误。
  2. Exception:表示可以被程序处理的异常情况。
        Throwable
         /     \
      Error   Exception
                 \
            RuntimeException

3. 检查型异常vs非检查型异常 #

3.1 检查型异常(Checked Exceptions) #

  • 除了RuntimeException及其子类之外的所有异常。
  • 必须在代码中显式处理或声明抛出。
  • 例如:IOException, SQLException

3.2 非检查型异常(Unchecked Exceptions) #

  • RuntimeException及其子类。
  • 不需要在代码中显式处理。
  • 例如:NullPointerException, ArrayIndexOutOfBoundsException

4. 异常处理 #

4.1 try-catch 块 #

try {
    // 可能抛出异常的代码
} catch (ExceptionType1 e1) {
    // 处理 ExceptionType1
} catch (ExceptionType2 e2) {
    // 处理 ExceptionType2
} finally {
    // 无论是否发生异常都会执行的代码
}

4.2 多重捕获 #

Java 7引入的特性,允许在一个catch块中捕获多个异常:

...