1 package org.sapia.console; 2 3 import java.io.*; 4 5 6 12 public class Console { 13 public static final String DEFAULT_PROMPT = ">>"; 14 private BufferedReader _in; 15 private PrintWriter _out; 16 private String _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 msg) { 40 _out.print(msg); 41 _out.flush(); 42 43 return this; 44 } 45 46 public Console println(String msg) { 47 _out.println(msg); 48 _out.flush(); 49 50 return this; 51 } 52 53 public String input(String 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 prompt) { 66 _prompt = prompt; 67 } 68 69 public String readLine() throws IOException { 70 return _in.readLine(); 71 } 72 } 73 | Popular Tags |