In this Lab we will learn about reading and writing to files. We’ll also get some practice reading the java docs.
Then you will write a program to analyze a set of presidential speeches.
- Note: If you aren’t able to click the links and see more content on this web page, try opening it in a different browser (e.g., Chrome).
Deadline: Wednesday 10/31
Please read this fully. You might learn something.
You can work individually, or with at most one other person.
First, get the files. Use this link to create one git repository per team. Once you’ve created the repository, go to its Settings Page on github and add your partner as a collaborator. This will allow both of you to push to the same repository.
IMPORTANT:
Part of the goal of this lab is to get you more comfortable working with the official Java documentation. Therefore, the only websites you are allowed to visit while working on this are my website and websites that start with http://docs.oracle.com/
. No googling stack overflow!
Opening Text Files to Read
First we will try to read from a file. You may have done this before, or you may know that a quick google search will give you code that you can copy and paste. Let’s pretend that’s not the case, and figure it out from scratch.
The most basic reference point for any Java question is the “Java Docs”. These give reasonably detailed explanations for every class and function in the entire Java library. Very handy. We will use the Java 6 docs since you are likely to have at least that version installed.
Open this in another window/tab: Java 6 API
We want to read from a file, which is a basic form of Input/Output, so we will have to find classes related to that. The top left of the window lists all of the packages in the core Java library. Find the java.io
package. Clicking this changes the bottom left frame so that it becomes a list of all the classes in that particular package. Click the java.io link at the very top of the bottom left frame to see the package summary.
At the top of this view we get a list of the Interfaces supported by this package, followed by all of its classes. As you can see, there are a lot of classes related to working with files.
Read through the list of classes in java.io
. If we want to write a program to read through a text file and print the lines in it to the screen, what classes do you think might be useful?
Record your answer in your README.md file, then click here.
These might be useful:
- FileReader - low level functionality for opening and reading from files stored on disk.
- BufferedReader - a wrapper class for
FileReader
to make it more efficient
- LineNumberReader - a subclass of
BufferedReader
to simplify tracking line numbers when reading from files
It sounds like BufferedReader
will definitely be of use to us. Click on the class name to see the class’s documentation. This gives a list of all the functions inside the class, but also provides an overview of what it does and an example of how to instantiate an object of its type.
From the documentation, why do we use BufferedReader
combined with the FileReader
class?
Record your answer in your README.md file, then click here.
The Buffered Reader
class is a wrapper that goes around another class like FileReader
. BufferedReader
makes the accesses to a file more efficient by reading a large portion of the file into a memory buffer all in one call and then letting the programmer access the contents of this buffer one character or line at a time. This is much more efficient than directly reading the contents of a file one character at a time.
Use CodeAnywhere to create a new java class named SimpleReader
with a main()
method. Paste into your main function the sample code from the BufferedReader docs that shows how to create a new object.
Click to see the code you should have added
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
If you try to compile this code you will get a java.io.FileReader
import error. To avoid this, add import java.io.*;
to include all of the libraries we need, or import each class you are using individually. Compile again.
We have fixed our import errors, but we now have a new error. The error is that we are not handling a possible exception caused by file IO (e.g., if the file doesn’t exist or is empty). To catch an exception we need to add a try/catch block around our code.
At this point, you might normally search google for a similar example. Instead, let’s look at our textbook. Searching the index of the textbook for FileReader gives us this page from the book, which explains the complete code to read from a file and print it to the screen.
SimpleReader
Using the textbook’s example as a guide, write a program which can read in the source code for your current program and print it to the screen, with a line number in front of each line.
When I run my program, the first few lines it displays are:
1: import java.io.BufferedReader;
2: import java.io.FileNotFoundException;
3: import java.io.FileReader;
Your code should be in the file SimpleReader.java. Commit it to github and push it to your repository before continuing on.
Once you have gotten your code working, click here to continue (or for a hint).
Here is sample output from my program:
1: import java.io.BufferedReader;
2: import java.io.FileNotFoundException;
3: import java.io.FileReader;
4: import java.io.IOException;
5:
6:
7: public class SimpleReader {
8:
9: public static void main(String[] argv) {
10:
11: try {
12: BufferedReader in
13: = new BufferedReader(new FileReader("SimpleReader.java"));
14:
15: String line = in.readLine();
16: int i=1;
17:
18: while(line != null)
19: {
20: System.out.println(i + ": " + line);
21: i++;
22: line = in.readLine();
23:
24: }
25:
26: } catch (FileNotFoundException e) {
27: System.out.println("Error! File not found!");
28: e.printStackTrace();
29: } catch (IOException e) {
30: System.out.println("Error reading from file!");
31: e.printStackTrace();
32: }
33:
34: }
35: }
Working with Strings
When reading text files it is common to have to manipulate the strings you get in some way. Like C, Java has many functions to help us work with Strings. The String class is part of the java.lang
package—find its documentation in the Java Docs and look through the functions provided there.
Does Java have functions to…
- Convert a string to upper case?
- Find a substring inside a string?
- Test if a string is a palindrome?
- Replace a character with another?
(answer these for your own edification, you do not need to submit the results)
Click here after you have explored the docs to try to find answers.
Parsing Text Files
Now let’s try reading from text files to do something more interesting. I would consider this a relatively easy problem if it were on one of your in-class programming exams. See how you do on it, and submit your code via github when you finish.
You have been hired by some historians to perform an analysis of past presidential speeches. The group seeks to understand how the words used in political speeches have changed over time. They would like you to analyze a set of inauguration addresses made by US presidents and calculate the average length of each word in a speech, as well as the total number of words. Write a program which reads through a speech stored in a text file (examples provided below) and calculate this information. Print the results to the screen. Assume that the first line of each file is the name of the president who gave the speech and the second line is the date of the speech. Print this information along with your output, but do not include it in your calculations.
There are many ways to do this. You could use a for
loop and the charAt()
method in the String class to find each word. Or you could use the split()
function inside the string class to take a line and get back an array with each word.
Inaugural addresses from some of our great (or perhaps not so great) presidents:
Speeches from: Miller Center Presidential Speeches. These files are already included in your starter github repo.
Submission:
- Put your code in a file named
SpeechReader.java
- Be sure your code has a comment with your name and your partner’s name
- Paste the output of your program when processing each of the files above into your README.
If you do not finish before class ends, you have until Wednesday 10/31 to submit the final code. If you finish the program before class ends, you can show your output to the TA and leave early. We will be extending this code during an upcoming lecture.