Java中IO操作:文件读写及错误处理

原创 不念不忘少年蓝@ 2024-12-01 15:51 193阅读 0赞

在Java中,IO(Input/Output)操作主要包括文件的读写和相关的错误处理。

  1. 文件读写:

    • FileReaderFileWriter:这两个类是用于文本文件的读写。例如,创建一个文件并写入内容:
      ```java
      // 创建一个文件对象
      File file = new File(“example.txt”);

    // 如果文件不存在,则创建它
    if (!file.exists()) {

    1. file.createNewFile();

    }

    // 使用FileWriter写入内容
    try (FileWriter writer = new FileWriter(file)) {

    1. String content = "This is an example";
    2. writer.write(content);

    } catch (IOException e) {

    1. System.out.println("Error occurred while writing to file: " + e.getMessage());

    }

    • BufferedReaderPrintWriter:这些类用于处理二进制文件。例如,读取和写入二进制数据:
      ```java
      try (FileInputStream fis = new FileInputStream(“example.bin”);

      1. BufferedReader br = new BufferedReader(new InputStreamReader(fis)))) {

      String line;
      while ((line = br.readLine()) != null) {

      1. // 处理读到的行
      2. System.out.println(line);

      }
      } catch (IOException e) {
      System.out.println(“Error occurred while reading file: “ + e.getMessage());
      }

    • OutputStreamInputStream:这些类用于处理数据,但不直接操作文件。例如,写入和读取字节:

      1. try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
      2. ByteArrayInputStream bis = new ByteArrayInputStream("Hello, World!".getBytes())) {
      3. byte[] bytes = bis.readAllBytes();
      4. System.out.println("Read bytes: " + new String(bytes)));
      5. bos.write("This will be appended.".getBytes());
      6. byte[] appendedBytes = bos.toByteArray();
      7. System.out.println("Appended bytes: " + new String(appendedBytes)));
      8. } catch (IOException e) {
      9. System.out.println("Error occurred while reading or writing bytes: " + e.getMessage());
      10. }
      11. }
  2. 错误处理:

  • try-catch 语句:用于捕获和处理可能抛出的异常。
  • IOException:Java中所有输入/输出操作可能会抛出此类异常。

示例:

  1. import java.io.*;
  2. // 文件读写时可能出现的错误
  3. class FileOperationError extends IOException {
  4. public FileOperationError(String message) {
  5. super(message);
  6. }
  7. }
  8. try {
  9. // 创建一个文件对象
  10. File file = new File("example.txt");
  11. // 试图写入内容,可能会抛出FileNotFoundException
  12. try (FileWriter writer = new FileWriter(file)) {
  13. String content = "This is an example";
  14. writer.write(content);
  15. } catch (IOException e) {
  16. System.out.println("Error occurred while writing to file: " + e.getMessage());
  17. throw new FileOperationError("Failed to write to file due to I/O error", e);
  18. }
  19. // 试图读取文件,可能会抛出FileNotFoundException
  20. try (BufferedReader reader = new BufferedReader(new FileReader(file)))) {
  21. String line;
  22. while ((line = reader.readLine()) != null) {
  23. System.out.println(line);
  24. }
  25. } catch (IOException e) {
  26. System.out.println("Error occurred while reading from file: " + e.getMessage());
  27. throw new FileOperationError("Failed to read from file due to I/O error", e);
  28. }
  29. } catch (FileOperationError e) {
  30. System.out.println(e.getMessage());
  31. // 如果需要,可以在此处进一步处理错误
  32. }

这段代码展示了如何使用try-catch语句进行文件操作的错误处理。

文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。

发表评论

表情:
评论列表 (有 0 条评论,193人围观)

还没有评论,来说两句吧...

相关阅读