Tutorial Addendum On Java - Beheading Of Java Programs
Execution Console
When the JVM is active a Java appliance program, it needs a animate window
to provides 3 predefined ascribe and achievement streams to the Java program:
to:
- System.in: Accepted ascribe beck affiliated to the animate window.
- System.out: Accepted achievement beck affiliated to the animate window.
- System.err: Accepted absurdity beck affiliated to the animate window.
When you use J2SDK amalgamation to assassinate a Java program, the animate window is the
same window area you entered the "java" command.
Note that all 3 accepted streams are authentic as a accessible changeless capricious of the "System"
class. We accept acclimated the "System.out" in our first Java program, Hello.java,
to book the accost bulletin on the animate window. Actuality is addition program that
uses all 3 accepted streams:
/**
* Reverser.java
* Absorb (c) 2002 by Dr. Yang
*/
import java.io.*;
public chic Reverser {
accessible changeless abandoned main(String[] arg) {
InputStream in1 = System.in;
InputStreamReader in2 = new InputStreamReader(in1);
BufferedReader in = new BufferedReader(in2);
PrintStream out = System.out;
PrintStream err = System.err;
err.println("This program reverses the appearance positions of ");
err.println("each band from the accepted ascribe stream.");
err.println("Enter . to end the program.");
try {
Cord s = in.readLine();
while (s!=null) {
char[] a = s.toCharArray();
if (s.equals(".")) break;
int n = a.length;
for (int i=0; i<n/2; i++) {
burn c = a[i];
a[i] = a[n-1-i];
a[n-i-1] = c;
}
out.println(new String(a));
s = in.readLine();
}
} bolt (IOException e) {
err.println(e.toString());
}
}
}
Note that:
- The "System.in" is an "InputStream" object, which alone offers methods to apprehend in
information as bytes. To be able to apprehend as one "String" per line, I charge to convert
"System.in" to a "BufferedReader" object.
- The "while" bend should end if "System.in" alcove the end of the stream.
However, HotSpot for Windows gives you no way to announce the end of the accepted
input beck from the key board. So I acclimated a appropriate pattern, one and alone one
character . in a line, to end the loop.
When I ran the program with the afterward command:
c:j2sdk1.4.1_01injava -cp . Reverser
and entered the afterward argument from key board:
Fish, I adulation you and account you actual much.
But I will annihilate you asleep afore this day ends.
.
I got the afterward on the animate window:
This program reverses the appearance positions of
each band from the accepted ascribe stream.
Enter . to end the program.
Fish, I adulation you and account you actual much.
.hcum yrev uoy tcepser dna uoy evol I ,hsiF
But I will annihilate you asleep afore this day ends.
.sdne yad siht erofeb daed uoy llik lliw I tuB
.
Note that:
- The first 3 curve are from the "System.err" stream.
- The 4th, 6th, and 8th curve are copies of the argument curve of the "System.in" stream,
because the animate window is alveolate aggregate I typed in on the keyboard.
- The 5th and 7th curve are from the "System.out" stream.
Windows arrangement can aswell alter the animate ascribe and achievement to files.
First, I adored the 2 curve of argument and the . into a book alleged input.txt,
then run the afterward command:
c:j2sdk1.4.1_01injava -cp . Reverser < input.txt > output.txt
I alone got the argument from the "System.err" on the animate window:
This program reverses the appearance positions of
each band from the accepted ascribe stream.
Enter . to end the program.
The output.txt book contains:
.hcum yrev uoy tcepser dna uoy evol I ,hsiF
.sdne yad siht erofeb daed uoy llik lliw I tuB
|
Tags: program, system, character, window, positions, connected, console, streams, command, lines, public, string system, program, stream, console, standard, window, input, println, output, lines, string, character, following, positions, reverses, reverser, execution, connected, command, public, streams, bufferedreader, , console window, input stream, standard input, err println, system out, system err, line from, lines are, character positions, reverses the, stream connected, program reverses, java program, standard input stream, input stream enter, yad siht erofeb, siht erofeb daed, uoy llik lliw, ofeach line from, character positions ofeach, console window system, dead before this, console window this, window this program, positions ofeach line, |
Also see ...
PermalinkArticle In : Computers & Technology - java