KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > console > Console


1 package org.sapia.console;
2
3 import java.io.*;
4
5
6 /**
7  * An basic command-line console.
8  *
9  * @author Yanick Duchesne
10  * 29-Nov-02
11  */

12 public class Console {
13   public static final String JavaDoc DEFAULT_PROMPT = ">>";
14   private BufferedReader _in;
15   private PrintWriter _out;
16   private String JavaDoc _prompt = DEFAULT_PROMPT;
17
18   public Console(InputStream in, OutputStream out) {
19     _in = new BufferedReader(new InputStreamReader(in));
20     _out = new PrintWriter(out, true);
21   }
22
23   public Console() {
24     this(System.in, System.out);
25   }
26
27   public PrintWriter out() {
28     return _out;
29   }
30
31   public BufferedReader in() {
32     return _in;
33   }
34
35   public void setOut(PrintWriter out) {
36     _out = out;
37   }
38
39   public Console print(String JavaDoc msg) {
40     _out.print(msg);
41     _out.flush();
42
43     return this;
44   }
45
46   public Console println(String JavaDoc msg) {
47     _out.println(msg);
48     _out.flush();
49
50     return this;
51   }
52
53   public String JavaDoc input(String JavaDoc msg) throws IOException {
54     prompt();
55     println(msg);
56
57     return readLine();
58   }
59
60   public void prompt() {
61     _out.print(_prompt);
62     _out.flush();
63   }
64
65   public void setPrompt(String JavaDoc prompt) {
66     _prompt = prompt;
67   }
68
69   public String JavaDoc readLine() throws IOException {
70     return _in.readLine();
71   }
72 }
73
Popular Tags