109 -- Lab 3: Reading Files
Nov 15
Here is the problem to work on this week:
- take the name of a file to be read from the command line
- create an array of size 5 of type char.
- read a file character by character into the 5 long array until there are no more characters in the file. There are some rules for filling the the array:
- put the first character into position 0 of the array.
- After that put the character you just read into the array in the position after the previous character except:
- if the previous character was put into position 4, put the new character into position 0.
- if the ASCII value of the new character is 10, do not put it into the array
- if the new character is 'Z' do not put it in the array
For instance suppose that you have a file ZZ.txt which has the contents
asdfghjklmnbvcxz
Then if the program is run
java ShortRead ZZ.txt
the output should be
zbvcx
Here are several more examples
QWEZrtyu | yuErt |
asd | asd |
asd fgh | hsdfg |
Suggestion: immediately after creating the array fill it with ' ' (ie the space character).
Setup
Start by making a new directory for lab3.
Next make a file -- you name it -- with your name as the contents.
Reading a File
There are many ways to read from files in Java. In this lab you will be reading character by character, so you will need a fairly low-level reader. The Java "FileReader" class will work just fine. Here is an example of using FileReader to read a character by character from a file named "ham.txt". Copy this code into your Lab4 directory, adjust it to read the file you just made and try it out.
1 import java.io.FileReader;
2 import java.io.IOException;
3
4 public class ReadOne {
5 public static void main(String[] args) {
6 try {
7 FileReader r = new FileReader("ham.txt");
8 while (r.ready()) {
9 int int_read = r.read();
10 char char_read = (char) int_read;
11 System.out.println(int_read + " " + char_read);
12 }
13 }
14 catch (IOException e) {
15 System.out.println("Problem: " + e);
16 }
17 }
18 }
(Line numbers are included for the discussion below, they are not part of the code)
The Java FileReader class does exactly what you might expect, it reads files; to get it stated as on line 7, we give it the name of the file to be read. Then, just prior to actually reading anything, we ask the file reader, on line 8, if there is anything to read. If there is something to read, on line 9 we actually read it. The read returns the ASCII value of the character (see below), so on line 10 we convert that number into an actual character so that on line 11 we can print both the actual character and its ASCII value.
Java requires that anything that might have problems of a particular form be between "try" and "catch". For example, line 7 might have a problem if the file being opened did not exist. Similarly, line 9 might have a problem if the file disappeared between opening and line 9. If one of these bad things happen, the program immediately goes down to the catch (line 14) and executes whatever code is in the catch block (line 15). You should almost always have a print statement within the catch block. Sometimes it makes sense to have other things as well.
So, all of this code that does the actions you actually want is in a block between the try (line 6) and the catch (line 14). We will discuss "try" and "catch" in class. The easiest thing to do will be to follow the pattern in the ReadOne program. That is make "try {" the first thing in the main method and
catch (IOException e) {
System.out.println("Problem: " + e);
return;
}
the last.
ASCII
ASCII is a standard for storing/encoding characters. Basically, ASCII takes every standard character in english and associates it with a number. You can see a the full translation at https://www.asciitable.com/. Internally to Java there is little difference between the ASCII value of a character (which is a number between 0 and 127) and the character. So on line 9 of the code above, when the file reader returns an int, it is also effectively returning a char. Hence on line 10 the cast from int to char has basically no work to do. Because ints and chars are so similar, code like
int ii = 70;
boolean bb = ii=='A';
if perfectly legal. This simply asks if the ascii integer value of the character 'A' is equal to 70 (it is not 'A' has a value of 65).
Likewise, this is legal
char cc = 'e';
boolean bb = cc==120;
bb = 'f' == 120;
In both cases bb is set to false because the ASCII int values of 'e' and 'f' are 101 and 102.
Note that chars in Java are denoted by single quotes, unlike strings that are denoted by double quotes.
Update the program
So you have a program that read a file character by character. Recall that the remainder of the lab is to
- put the first character into position 0 of the array.
- After that put the character you just read into the array in the position after the previous character except:
- if the previous character was put into position 4, put the new character into position 0.
- if the ASCII value of the new character is 10, do not put it into the array
- if the new character is 'Z' do not put it in the array
Do that now.
Finally print out the final contents of the array.
Change your file
the file you made with your name, try changing its contents to each of the examples above. Make sure you program works for each.
What to Submit
Send your completed program, or as close as you got to completion, to gtowell@brynmawr.edu. As usual, work on this for no more than 80 minutes.