迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

修复 Java 错误 java.io.IOException: Stream Closed

作者:迹忆客 最近更新:2023/07/12 浏览次数:

本篇文章我们将找出在 Java 编程中编码时产生 java.io.IOException: Stream Closed 错误的可能原因。 我们还将借助代码示例探索两种可能的解决方案来修复此错误。


导致 java.io.IOException:Stream closed 的原因

示例代码(导致错误):

//import libraries
import java.io.FileWriter;
import java.io.IOException;

//Test Class
public class Test{

    //this method writes the given data into the specified file
    //and closes the stream
    static void writeToFile(String greetings,
                            String firstName,
                            String lastName,
                            FileWriter fileWriter) {

        String customizedGreetings = greetings + "! "+
                                     firstName + " " +
                                     lastName;
        try {
            fileWriter.write(customizedGreetings + "\n");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException exception) {
            exception.printStackTrace();
        }

    }//end writeToFile() method

    //main() method
    public static void main(String[] args) throws IOException {

        //creates a file in append mode and keeps it open
        FileWriter fileWriter = new FileWriter("Files/file.txt", true);

        //writeToFile() is called to write data into the file.txt
        writeToFile("Hi", "Mehvish", "Ashiq", fileWriter);
        writeToFile("Hello", "Tahir", "Raza", fileWriter);

   }//end main()

}//end Test class

让我们通过代码来查找导致 java.io.IOException: Stream Closed 错误的原因。 然后,我们将跳转到它的解决方案。

此代码片段使用位于 java.io 包中的 FileWriter 类,用于将字符形式的数据写入指定文件。 如果给定位置不存在指定的文件,它将创建该文件并使其保持打开状态。

如果文件已经存在,则 FileWriter 将替换它。

在 main() 方法中,我们调用 FileWriter 构造函数以追加模式创建指定文件,然后调用 writeToFile() 方法两次将给定数据写入 file.txt 文件。

第一次调用时, writeToFile() 方法将数据写入 file.txt,刷新 FileWriter 的数据,然后关闭它。 请注意,我们已经通过调用 close() 方法关闭了流。

在第二次调用时,FileWriter 对象无法找到它应该写入的文件,因为流已关闭。 因此,第二次调用 writeToFile() 方法会导致此错误。

有两种解决方案可以修复此错误。 下面给出了它们和代码示例。

通过创建新流修复 java.io.IOException:Stream closed 错误

第一个解决方案是每当我们想要写入指定文件时,通过将 FileWriter 对象移动到 writeToFile() 函数中来创建一个新流。

示例代码:

//import libraries
import java.io.FileWriter;
import java.io.IOException;

//Test class
public class Test{

    //this method writes the given data into the specified file
    //and closes the stream
    static void writeToFile(String greetings,
                            String firstName,
                            String lastName) throws IOException{

        FileWriter fileWriter = new FileWriter("Files/file.txt", true);

        String customizedGreetings = greetings + "! "+
                firstName + " " +
                lastName;

        fileWriter.write(customizedGreetings + "\n");
        fileWriter.flush();
        fileWriter.close();


    }//end writeToFile()

    //main()
    public static void main(String[] args){

        //writeToFile() is called to write data into the file
        try {
            writeToFile("Hi", "Mehvish", "Ashiq");
            writeToFile("Hello", "Tahir", "Raza");
        }catch (IOException e){
            e.printStackTrace();
        }

    }//end main()

}//end Test class

输出(file.txt 中的数据):

Hi! Mehvish Ashiq
Hello! Tahir Raza

通过将 close() 移动到 writeToFile() 之外来修复 java.io.IOException: Stream closeed 错误

第二种解决方案是将 close() 方法移到 writeToFile() 函数之外,与解决方案 1 相比,这似乎是一个好方法。

示例代码:

//import libraries
import java.io.FileWriter;
import java.io.IOException;

//Test Class
public class Test{

    //this method writes the given data into the specified file
    static void writeToFile(String greetings,
                            String firstName,
                            String lastName,
                            FileWriter fileWriter) {

        String customizedGreetings = greetings + "! "+
                                     firstName + " " +
                                     lastName;
        try {
            fileWriter.write(customizedGreetings + "\n");
            fileWriter.flush();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }//end writeToFile()

    //closes the stream
    static void cleanUp(FileWriter fileWriter) throws IOException {
        fileWriter.close();
    }//end cleanUp()

    //main()
    public static void main(String[] args) throws IOException {

        //create the file in the append mode and keep it open
        FileWriter fileWriter = new FileWriter("Files/file.txt", true);

        //writeToFile() is called to write data into the file.txt
        writeToFile("Hi", "Mehvish", "Ashiq", fileWriter);
        writeToFile("Hello", "Tahir", "Raza", fileWriter);

        //close the stream
        cleanUp(fileWriter);

   }//end main()

}//end Test class

输出(file.txt 中的数据):

Hi! Mehvish Ashiq
Hello! Tahir Raza

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Java 异常 Java.IO.IOException: Connection Reset by Peer

发布时间:2023/07/16 浏览次数:1586 分类:Java

本篇文章介绍 Java 的 java.io.IOException: Connection reset by peer。Java 中 java.io.IOException: Connection reset by peer IOException 表示读取或写入文件或访问文件系统时可能发生的任何输入输出异常。

修复 Java 中错误 java.io.IOException: No Space Left on Device

发布时间:2023/07/14 浏览次数:557 分类:Java

本篇文章我们将找出Java编程时出现java.io.IOException: No space left on device错误的原因。 此外,我们还将了解消除此错误的可能解决方案。java.io.IOException: No space left on device 错误的原因和可能的解决

Java 异常 Java.IO.Ioexception

发布时间:2023/07/13 浏览次数:634 分类:Java

本篇文章介绍了 Java 中的 java.io.ioException。Java 中的 java.io.IOException IOException是Java中最常见的异常。

修复 Java 错误 java.lang.UnsupportedClassVersionError

发布时间:2023/07/12 浏览次数:342 分类:Java

本篇文章介绍了 Java 中的 Exception in thread main java.lang.UnsupportedClassVersionError 错误。java.lang.UnsupportedClassVersionError的原因 UnsupportedClassVersionError 是 ClassFormatError 异常的子类,当 JVM 尝试读取类并发现

修复 Java 错误 Java.Net.SocketException: Permission Denied

发布时间:2023/07/11 浏览次数:742 分类:Java

本篇文章介绍了 Java 中的 java.net.SocketException:Permission denied 错误。Java中出现 java.net.SocketException: Permission returned 错误的原因 SocketException 通常在网络连接出现问题时发生。

修复 Java 错误 $' ': Command Not Found

发布时间:2023/07/11 浏览次数:145 分类:Java

本篇文章介绍了 Java 中的 $'\r': command not found 错误。Java 中 $'\r': command not found 的原因当我们尝试在 Cygwin 等平台的 Linux 平台上运行 Unix 风格的命令时,会出现错误 $'\r': command not find 。

解决 Java 中 java.io.IOException: Broken Pipe 错误

发布时间:2023/07/10 浏览次数:1059 分类:Java

本文介绍了 java.io.IOException: Broken pipeline 异常的原因,并提供了在 Java 中修复该异常的解决方案。 但在此之前,让我们先看一下 java.io.IOException: Broken pipeline 异常。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便