Java io stream closed error -
Java io stream closed error -
so took me while prepare errors debug , run programme wrote intro java class. giving me next error after first input.
exception in thread "main" java.io.ioexception: stream closed @ sun.nio.cs.streamdecoder.ensureopen(streamdecoder.java:46) @ sun.nio.cs.streamdecoder.read(streamdecoder.java:148) @ java.io.inputstreamreader.read(inputstreamreader.java:184) @ java.io.bufferedreader.fill(bufferedreader.java:161) @ java.io.bufferedreader.readline(bufferedreader.java:324) @ java.io.bufferedreader.readline(bufferedreader.java:389) @ statsdemo.main(statsdemo.java:54)
i wrote below println according comments , instructions. not sure wrong. supposed inquire come in file numbers.txt after come in file, gives me 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 programme calculates statistics" + "on file containing series of numbers"); system.out.print("enter file name: "); filename = keyboard.nextline();
second loop you're reading :
line = in.readline();
while open stream called in2.
i.e you're reading wrong , closed stream.
also, practice should close top reader instead of inner i.e should utilize in.close() instead of file.close();
purpose of flush:
if within source of printwriter you'll see uses 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 send output underlying output stream. keeps output saved in memory until flushed. flush happens automatically when output contains new line. otherwise need flush buffer manually create sure output gets written.
java ios loops while-loop
Comments
Post a Comment