Click to See Complete Forum and Search --> : End of InputStream in DOS?


Harish Chandra
February 25th, 2000, 06:05 AM
What is the end of InputStream in DOS? I am running the following program in MS-DOS prompt under Windows 95 (code picked up from "The Java Programming Language" by Ken Arnold and James Gosling). If I press CTRL+C or CTRL+Z, it ends the program without printing the output. I have tried all other control characters but could not determine end of the input.

Here is the code:

/* CountBytes.java
Illustrates the use of InputStream.
From: The Java Programming Language, pg 228-229.
*/

import java.io.*;

class CountBytes {
public static void main (String [] args) throws IOException {
InputStream in;

// Don't know what key to press to mark the end-of-input from the DOS prompt.

if (args.length == 0)
in = System.in;
else
in = new FileInputStream (args [0]);

int total = 0;
while (in.read () != -1)
total ++;

System.out.println (total + " bytes.");
}
}

Jan Meeuwesen
February 25th, 2000, 07:19 AM
Hi there,
Here's a little example that use 'q' to stop the input.

import java.io.*;

class BRRead {
public static void main(String args[])
throws IOException
{
int total = 0;
char c;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");

// read characters
do {
c = (char) br.read();
total++;
} while(c != 'q');
System.out.println (total + " bytes.");
}
}




Greets,
Jan Meeuwesen.
( Don't forget to vote this answer (bottom right) )