Class - October 21
Blanca had her baby early this morning. She said that she was doing fine and that everything went well.
Today we will go over files. This includes how to read information in from a file, and how to write information out to a file. For today you will need to learn the way that we show you below- there are other ways to accomplish this- see that Java Tutorial for alternative ways. Also, we will cover only reading and writing files today. For more methods you can invoke on files, please check out the Java API (File class, PrintStream class, etc.).
We will cover Arrays next class- please read that section of your book before
Tuesday.
Before we start, some comments on the project and the quizzes...
ICS111 Notes by Blanca
Input files are the files that we read from. They are input for our program.
Output files are files that we write to. They are the output of our program.
When a program tries to open/read an input file:
If it cannot find it, it will throw a FileNotFoundException. The code where an input file is opened is therefore a high risk code that should be inside a try statement.
When a program tries to write to a specified output file:
If the file doesn’t exist, it will create it. No exception will be thrown.
If the file exists, it will overwrite it. No exception will be thrown, no warning will be issued.
The name of a file is a String. The file name may or not have an extension. Alphanumeric characters and periods are accepted. Keep in mind that you should name your file according to its contents and use. Here are some valid file names:
file.txt
myFile.dat
someStuff
something.mine
To use an Input file inside a JAVA program:
1. File f = new File("file.txt");
2. FileInputStream fis = new FileInputStream(f);
3. InputStreamReader isr = new InputStreamReader(fis);
4. BufferedReader br = new BufferedReader(isr);
The file is declared. The file will be called f inside the program but it will linked to a file called “file.txt” which is in the same directory as the java program.
Steps 2, 3 and 4 are always required. Just as when we were reading from the user we had some required steps in this case is the same.
String s = "";
s = br.readLine( );
Files contain characters that we cannot see. One of these characters is the end of line character <eol>. At the end of every line this character appears. This character is an indication to stop reading a particular line right there. Therefore the instruction br.readLine will read from the beginning-of-line <bol> all the way until the end-of-line. In the case of s= br.readLine( ); a line containing all the characters from the <bol> to the <eol> will be assigned to s so that we can use that information.
There is no need for closing files in JAVA. Once the program exits, all files in used are automatically closed and their contents are saved.
To use an Output file in a JAVA program:
1. String sFilename = "myFile";
2. File f = new File(sFilename);
3. FileOutputStream fos = new FileOutputStream(f);
4. PrintStream ps = new PrintStream(fos);
5. String oneLine="";
In this case we saved the filename inside a String. This can be done also for Input files. The filename can even be read from the user.
In step 2 the file is declared. Notice that files are declared exactly the same way if the file and it doesn’t matter if it is an input or an output file.
Steps 3 and 4 are needed in order to write to a file.
The PrintStream object is the one that contains the print an println methods. The same way that print and println are used to write to the screen, when they are related to an output file are used to write to a file.
Note: When declaring/using variables related to the
input file be sure that all risky code is inside a try statement to avoid
crashing. You must also consider variable scope otherwise your variables
will not be recognized outside a block of code.
File Input Reader: File f =
new File("file.txt"); br.readLine will read a line of the file |
Traditional 'System.in'
reader: InputStreamReader isr = new
InputStreamReader(System.in);
br.readLine will read a line from the user |
File Writer: File fout = new
File ("outputFile.txt"); ps.println() will write a line of output to the file |
Traditional 'System.out'
writer: // don't need any extra code to set up System.out
System.out.println() will write a line of output to the screen |
Files.java (reads lines of Strings from one file, saves them to another, and counts the lines)
FileTest.java (reads lines of Strings from a file and prints them out)
FileTest2.java (reads input from the keyboard and saves it to a file)
Go home or to the lab and practice reading and writing to / from files. It is simple and you will be using it next week.
Also, next week we will cover Arrays. Please read that section of your book before Tuesday.