Java Console and File Input/Output Cheat Sheet

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:locationoutputfile.txt”)));
out.print(“Hello “);
File Input
BufferedReader in = new BufferedReader(new FileReader(“K:locationinputfile.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();


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s