File Input and Output CSC 1401: Introduction to Programming with Java Week 4 – Lecture 2 Wanda M. Kunkle
2 Why Use Files for Input/Output (I/O)? Data input at the keyboard and output to the screen is only stored temporarily. (At least that has been the case in the Java programs we have worked with so far.) Data input at the keyboard and output to the screen is only stored temporarily. (At least that has been the case in the Java programs we have worked with so far.) To store data permanently, we must write it to a file. (This is similar to creating an MS Word document and saving it under a specific file name.) To store data permanently, we must write it to a file. (This is similar to creating an MS Word document and saving it under a specific file name.)
3 Different Types of Files Data can be stored in one of two file types: Data can be stored in one of two file types: –Text files (called sequential files in your book) Sequences of characters Sequences of characters Can be read by human beings Can be read by human beings Can read and write to them using a text editor (e.g., Notepad) Can read and write to them using a text editor (e.g., Notepad) –Binary files Sequences of binary digits Sequences of binary digits Can only be read by computer programs Can only be read by computer programs Can only read and write to them using a computer program (e.g., a Java program) Can only read and write to them using a computer program (e.g., a Java program)
4 What Types of Files Will We Be Reading and Writing? Text files Text files –The kind that human beings can read –The kind that human beings can read
5 Sample Text File: MovieQuotes.txt Data: Strange. Part of me is sorry that she is dead. She brought me closer to humanity than I ever thought possible. And for a time I was tempted by her offer. Picard: How long a time? Data: Zero point eight six seconds....For an android that is nearly an eternity. Data: Strange. Part of me is sorry that she is dead. She brought me closer to humanity than I ever thought possible. And for a time I was tempted by her offer. Picard: How long a time? Data: Zero point eight six seconds....For an android that is nearly an eternity.
6 Sample Text File: MovieQuotes.txt Any idea what Star Trek movie the quotes are from? Any idea what Star Trek movie the quotes are from? But I digress … ;-) But I digress … ;-)
7 Steps to Read a Text File Open the file for input Open the file for input –This step positions the file pointer at the first character in the file. –The file pointer is a hidden variable that defines the position of the next input character in the file. Read the file character by character until the eof (end-of-file) character is encountered Read the file character by character until the eof (end-of-file) character is encountered –The eof character is a special character that marks the end of the file. –It is visible to computers but not to humans. Close the file Close the file
8 Methods for Reading a Text File in Java There are three provided by the software that comes with our book: There are three provided by the software that comes with our book: –in.read() –in.next() –in.more()
9 Methods for Reading a Text File in Java in.read() in.read() –Reads the next input character (or basic.eof ) –Moves the file pointer one character ahead –Example: // Read a single character and // advance file pointer char inputChar = in.read();
10 Methods for Reading a Text File in Java in.next() in.next() –Reads the next input character (or basic.eof ) –Does NOT advance the file pointer –Example: // Read a single character but // do not advance the file pointer char inputChar = in.next();
11 Methods for Reading a Text File in Java in.more() in.more() –Returns a true or false value –True if there is more input to be read from the file; false, otherwise –Returns the same value as: in.next() != basic.eof –Example: // Read the file character by character // as long as there is input to be read while (in.more()) { char inputChar = in.read(); }
12 Repetition Structures while while –Usage: Used to specify that an action is to be repeated as long as some condition remains true Used to specify that an action is to be repeated as long as some condition remains true May never execute if the result of the initial test is false May never execute if the result of the initial test is false –Format: while (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } –Format: while (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } –Example: int counter = 0; while (counter < 100) { // Loops as long as counter is less than 100 out.writeln(counter); // Display counter counter = counter + 1; // Increment counter by 1 }
13 Sample Programs Now let’s look at some sample programs that demonstrate reading text files: Now let’s look at some sample programs that demonstrate reading text files: –InputTextFile1.java InputTextFile1.java –InputTextFile2.java InputTextFile2.java
14 Steps to Write a Text File Open a file for output (e.g., “Out.txt”) Open a file for output (e.g., “Out.txt”) –Creates an empty file named “Out.txt,” if a file with that name does not exist –Overwrites the file named “Out.txt,” if a file with that name does exist Write text to the file character by character until "eof" (for end-of-file) is entered Write text to the file character by character until "eof" (for end-of-file) is entered Close the file Close the file
15 Sample Programs Now let’s look at a sample program that demonstrates writing a text file: Now let’s look at a sample program that demonstrates writing a text file: –OutputTextFile1.java OutputTextFile1.java
16 Steps to Copy a Text File Open one file for input (e.g., MovieQuotes.txt) Open one file for input (e.g., MovieQuotes.txt) Open a second file for output (e.g., CopyOfMovieQuotes.txt) Open a second file for output (e.g., CopyOfMovieQuotes.txt) Read the input file character by character until the eof (end-of-file) character is encountered Read the input file character by character until the eof (end-of-file) character is encountered Write the output file character by character as the input file is read Write the output file character by character as the input file is read Close the input file Close the input file Close the output file Close the output file
17 Sample Programs Now let’s look at a sample program that demonstrates copying one text file to another text file: Now let’s look at a sample program that demonstrates copying one text file to another text file: –CopyTextFile1.java CopyTextFile1.java