Java Console and File Input/Output Cheat Sheet (Carleton.edu)

Expanded to include spreadsheet output by G Howser 20171108

Console Output

System.out.print("Hello ");
System.out.println("world");

Console Input

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

File Output

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

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
out.close();

File Input

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

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();
  System.out.println(text);
}

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();