Java Console and File Input/Output Cheat Sheet (

Expanded to include spreadsheet output by G Howser 20171108

Console Output

System.out.print("Hello ");

Console Input

BufferedReader in = new BufferedReader(new InputStreamReader(;
String text = in.readLine();

File Output

PrintWriter out = new PrintWriter(new FileWriter("K:\\location\\outputfile.txt")));
out.print("Hello ");

File Output (spreadsheet)

PrintWriter out = new PrintWriter(new FileWriter("K:\\location\\outputfile.xls")));
out.print("cell1\t cell2\t cell3\t");// \t tabs to next field
out.print("last cell\n");//ends this row and goes to next row

File Input

BufferedReader in = new BufferedReader(new FileReader("K:\\location\\inputfile.txt"));
String text = in.readLine();

Converting input data

String text  = in.readLine();
int x = Integer.parseInt(text);
double y = Double.parseDouble(text);

Reading until EOF

while (in.ready()) {
  text = in.readLine();

Pulling apart delimited items on a line

String text = "Beggars in Spain*Nancy Kress*1992";
StringTokenizer tokenizer = new StringTokenizer(text,"*");
String title = tokenizer.nextToken();
String author = tokenizer.nextToken();
String year = tokenizer.nextToken();

String letters = "a b c d e f g h i j";
StringTokenizer tokenizer = new StringTokenizer(text," ");
String[] allText = new String[10];
int pos = 0;
while (tokenizer.hasMoreTokens())
      allText[pos++] = tokenizer.nextToken();