Why this code does not work if I do not uncomment the line System.out.print(" "); ?
3 cases:
System.out.print(" "); after outprint.write(var); results in: helloworlSystem.out.print(" "); before outprint.write(var); leads to: helloworld- without
System.out.print(" "); nothing is displayed
To summarize, I pass an instance of System.out (which is PrintStream ) to the out attribute of the FilterOutputStream object (which accepts OutputStream ).
import java.io.FilterOutputStream; import java.io.IOException; public class CodeCesar { public static void main(String[] args) { FilterOutputStream outputstream = new FilterOutputStream(System.out); String line = "hello world"; char[] lst_char = line.toCharArray(); for (char var : lst_char) { try { outputstream.write(var); System.out.print(" "); <--------- THIS LINE } catch (IOException e) { e.printStackTrace(); } } } }
source share