Java io stream closed error -


So do I fix all errors debug and wrote my introduction to Java class program took some time to run. But now it is giving me the following error after the first input.

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

I have written everything under println according to the comments and instructions. But I'm not sure what's wrong. It is supposed to ask to enter file numbers.txt, but after I file the file, it gives me that error.

  import java.text.DecimalFormat; Import java.util.Scanner; Import java.io * *; Public Category Stats {Public Static Zero Main (String [] Args) IOException throws {double sum = 0; Int count = 0; Double meaning = 0; Double stdDev = 0; Double gap; Decimal three formats = new passphrase ("0.000"); Scanner Keyboard = New Scanner (System.in); String filename; System.out.println ("This program calculates statistics" + "on a file with a series of numbers"); System.out.print ("Enter File Name:"); File name = keyboard.nextLine ();  

The second loop you are reading:

line = in .readLine ();

Your open stream is called in 2.

That is, you are reading from the wrong and closed stream.

In addition, as a good practice on the most reader instead should stop inside the place you should use in.close () instead of the file. Close ();

flush objective:

If you see you PrintWriter see inside source that uses an internal buffer:

  public Print-Witter (File File, String CSN) file notfound exception, unsupported encoding exception {false (new BufferedWriter (new output-storm water (new fileOutstrstr (file) CSN), false); }  

This buffer does not immediately send output to the built-in output stream. It keeps the output saved in the storage up to the flush. Usually the flush is automatically done when your output contains a new line otherwise you need to manually flush the buffer so that it ensures that your output is actually written.


Comments