Duplicate :
|
Name: nt126004 Date: 11/06/2001 java version "1.4.0-beta2" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77) Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode) The following naive Cat equivalent will gobble up the first 40 characters when fed interactively from the keyboard under MS Windows 95. Instructions: - Compile the enclosed Cat.java under MS Windows 95 javac Cat.java - Run it to get input from the keyboard java Cat - Input 41 characters and press <Enter> (on some systems it is more characters) - Input some more lines - Input <Ctrl>Z to end input - Watch the first 40 characters disappear in the output I bet it's something in the System.out in the Windows version of JDK 1.3.1: I can't observe this problem under Solaris/Intel, for example. If an OutputStream is used instead of a BufferedOutputStream, the effect is not observable. // Cat.java import java.io.*; /** This class implements a simplified UNIX cat utility. */ public class Cat { /** Copies a given buffered reader stream to the standard output. @param r buffered intput stream @param w buffered output stream @throws IOException on any I/O error */ static void cat(InputStream r,BufferedOutputStream w) throws IOException { int c; while ((c = r.read()) != -1) w.write(c); w.flush(); } /** Synopsis: <PRE> cat [file1] [file2] ... </PRE> Copies the given files onto the standard output. Without arguments, reads the standard input. @param args array of string arguments @throws IOException on any I/O error */ public static void main(String[] args) throws IOException { BufferedOutputStream w = new BufferedOutputStream(System.out); if (args.length > 0) for (int i = 0; i < args.length; i++) { BufferedInputStream r = new BufferedInputStream(new FileInputStream(args[i])); cat(r,w); r.close(); } else cat(System.in,w); } } (Review ID: 135069) ======================================================================