1

So it took me a while to fix all the errors from debug and run the program I wrote from my intro Java class. But now it is giving me the following error after first input.

Exception in thread "main" java.io.IOException: Stream closed
at sun.nio.cs.StreamDecoder.ensureOpen(StreamDecoder.java:46)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:148)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at StatsDemo.main(StatsDemo.java:54)

I wrote everything below println according to the comments and instructions. But I am not sure what is wrong. It is supposed to ask to enter the file numbers.txt but after I enter the file, it gives me that error.

import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.*;

public class StatsDemo
{
    public static void main(String [] args) throws IOException
    {
        double sum = 0;
        int count = 0;
        double mean = 0;
        double stdDev = 0;
        double difference;

        DecimalFormat threeDecimals = new DecimalFormat("0.000");
        Scanner keyboard = new Scanner (System.in);
        String filename;

        System.out.println("This program calculates statistics"
            + "on a file containing a series of numbers");
        System.out.print("Enter the file name:  ");
        filename = keyboard.nextLine();
3
  • I'll have to give your question itself more consideration, but as a side note, you should remove the throws IOException from your main method. While it technically allows you to avoid try/catch, there will never be an entity that you control calling main, so any exceptions will be unhandled, which is not good coding practice. Commented Oct 27, 2014 at 23:32
  • Could you point out the line where the exception is being thrown? Commented Oct 27, 2014 at 23:35
  • possible duplicate of java IO Exception: Stream Closed Commented Oct 27, 2014 at 23:39

1 Answer 1

1

Second loop you're reading from :

line = in.readLine();

While your open stream is called in2.

i.e you're reading from the wrong and closed stream.

Also, as good practice you should close the top most reader instead of the inner most i.e you should use in.close() instead of file.close();

Purpose of the Flush:

If you look inside the source of PrintWriter you'll see it uses an internal buffer:

public PrintWriter(File file, String csn)
throws FileNotFoundException, UnsupportedEncodingException
{
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn)),
     false);
}

That buffer doesn't immediately send the output to the underlying output stream. It keeps the output saved in memory until flushed. Usually the flush happens automatically when your output contains a new line. Otherwise you need to flush the buffer manually to make sure your output actually gets written.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I changed them and I no longer get stream error. But I don't seen any results in my result.txt file. What am I doing wrong?
Try flushing the output stream before closing: outputFile.flush(); outputFile.close();
Thank you very much. It works now. I was wondering what is the purpose of flush? Just for future reference. And thanks again.
Added explanation for flush

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.