week07 - Week 7: Input and Output CS 177 1 Everyone loves

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 177 Week 7: Input and Output 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Everyone loves System.out.println() Now we are going to talk a little bit about output You have a lot of experience with System.out.println() and System.out.print() You feel comfortable with them and can output nicely formatted int s, double s, boolean s, char s, and String s with them 2
Background image of page 2
What about files? Printing things to the screen and the DrJava console is all well and good Sometimes you need to store something for later Sometimes you get tired of typing the same thing over and over again For these and other reasons, we put things into more permanent storage: files 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Output redirection When we run a Java program from the command line, we can send all the output it makes to a file, instead of to the screen To do this, you use the > operator to redirect the output to the file name you choose The following command runs program Test and sends its output to out.txt $ java Test > out.txt 4
Background image of page 4
By now, you know StdIn But, a refresher never hurts Method Use boolean isEmpty() Tells if there are more values int readInt() Read in the next int double readDouble() Read in the next double boolean readBoolean() Read in the next boolean char readChar() Read in the next char String readString() Read in the next String String readLine() Read in a whole line String readAll() Read in all remaining input 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
StdIn to read from files too Just as we can write data to a file, we can read data from a file, using the < operator The program “pretends” that a user is typing in the data that it gets from the file Now, the StdIn methods like isEmpty() and readAll() should make more sense The following command runs program Test and reads in input from in.txt $ java Test < in.txt 6
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/23/2012 for the course CS 177 taught by Professor Staff during the Spring '08 term at Purdue University.

Page1 / 26

week07 - Week 7: Input and Output CS 177 1 Everyone loves

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online