The task is to create your own class Stream, which would output data from the system input stream to the output stream and to the file using the BufferedStream. Data from the BufferedOutputStream is not output. How to output data from the BufferedStream? When writing to a file, is it correct to use the BufferedStream in order not to access the file many times or does java do it automatically?

import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class DoubleEndedStream { InputStream theInput; OutputStream theOutput; public static void main(String[] args) throws IOException, FileNotFoundException { DoubleEndedStream sr = new DoubleEndedStream(System.in, System.out); sr.doublingTheStream(); } public DoubleEndedStream(InputStream in, OutputStream out) { theInput = in; theOutput = out; } public void doublingTheStream() throws IOException, FileNotFoundException { try { FileOutputStream fos = new FileOutputStream("C:\\log.txt"); BufferedOutputStream bout1 = new BufferedOutputStream(fos); BufferedOutputStream bout2 = new BufferedOutputStream(theOutput); try { while (true) { int datum = theInput.read(); if (datum == -1) break; bout1.write(datum); bout2.write(datum); } bout1.flush(); bout2.flush(); } catch (IOException e) { System.err.println("Couldn't read from System.in!"); } bout1.close(); bout2.close(); fos.close(); } catch (FileNotFoundException e) { System.err.println("Couldn't find log.txt"); } } } 

    1 answer 1

    while (true) {int datum = theInput.read (); if (datum == -1) break;

    datum does not take the value -1, because it does not reach the end of the System.in stream. for normal operation, another verification condition is necessary (for example, pressing a special character)

    • And how can you without the use of specials. characters not to lose data that is not derived from the buffer? - cadmy