In this lab we will look at Java's built-in, and somewhat primative, I/O operations and how they must be used with Java's exception handling mechanism.
Download the starter Java archive
IOExcept.jar
and save it into the
directory csci/201.
Now unjar the Java archive.
This should
give you a directory csci/201/IOExcept
with two files IOExcept.java and
iolabin.txt.
The Java code you use to start this lab is contained in IOExcept.java. You're going to write a program to process the data contained in the file iolabin.txt.
Go ahead and look at iolabin.txt. It's just a simple text file with four lines.
201 202 R2D2 333
Now let's return to IOExcept.java.
Using Java I/O isn't easy. Take a careful look at the
code within the main
method of IOExcept
.
The first two Java statements create a File
object
referenced by the variable inFile
.
Because the File object is a representation of
a file name, rather than of a file, there is no problem in
executing these two statements even when iolabin.txt
does not exist.
String inName = "iolabin.txt" ; File inFile = new File(inName) ;
Now look at the three lines of Java code inside the
try
block.
InputStream inStream = new FileInputStream(inFile) ; InputStreamReader inReader = new InputStreamReader(inStream) ; BufferedReader inLineIO = new BufferedReader(inReader) ;
The first line opens the
file referenced by inFile
and creates an associated
InputStream object named
inStream
. If the file does not
exist, the FileInputStream
constructor
will throw FileNotFoundException and the
remainder of the try
block will be skipped.
By the way, because FileInputStream
is a subclass of
InputStream
, it is acceptable to assign a
FileInputStream object to an InputStream
variable.
An InputStream
is a simple sequence of
bytes, eight-bit numbers. A few years
ago it was OK to consider characters
as bytes interpreted under the good ole
ASCII (American Standard Code for Information Interchange) standard;
but today our programs are supposed to
be able to process the characters of French, Chinese,
or Cherokee.
Consequently, we can no longer consider a byte
to be a character. (Even in the old days, there
was also the problem that bytes were eight bits and ASCII characters
were seven bits, but that didn't seem to bother anyone.)
The second line of Java within the try
block
creates a InputStreamReader object, or a sequence
of characters, from a InputStream
object.
But we're still not there. We want to read our file one line at a time.
The third Java statement
creates a BufferedReader object, which can handle lines of
text, from the InputStreamReader
object.
By now, you probably appreciate why some introductory Java textbooks introduce a non-standard Keyboard class so that students are protected from naked Java I/O.
Finally, we get to a while
loop that reads and then
prints lines from the file. Notice, the read-then-test
style of programming required to make sure the loop terminates
after the last line of text is read. When this happens the
readLine method returns null
and escapes the loop.
Now that you've studied the code, you should be convinced that it will read and print all the lines of the file. Go ahead and run the application.
It is quite possible that,
even though the file iolabin.txt clearly exists,
the FileInputStream
constructor
threw a FileNotFound
exception and your program printed the line:
No such file: iolabin.txt (No such file or directory)
This can happen because Java may be looking for iolabin.txt in some directory other than your csci/201/IOExcept directory. To correct this problem, you will need to set the working directory for your program. This isn't easy.
First, right-click on IOExcept.java within your Explorer window and then select Properties from the pull-down menu. This should raise a pop-up widow labeled Properties of IOExcept. Select the Execution tab at the bottom of this window.
Now, click on External Execution. Three dots will appear to the right of External Execution. Click on the dots. A pop-up labeled Executor should now appear. Select its Expert tab. Now press on the blank space, just to the right of Working Directory. It's another three dots.
Click on the three dots to bring up a window labeled
Working Directory. Now choose csci/201/IOExcept
as your working directory.
If you are successful, the Executor window should look something like this:
Now you should be able to run your program to produce the following output.
Line #1: 201 Line #2: 202 Line #3: R2D2 Line #4: 333
Right now the while
loop merely counts the number
of lines in iolabin.txt. Notice that most,
but not all, of the lines of iolabin.txt contain
single integers. In this lab you will be asked to add
all those integers and print their sum.
Your first challenge will be translating a Java String
,
such as "201"
, into a Java int
,
such as 201
.
There is a static
Java method of
Integer
called parseInt
that will perform this operation for you; however, it must be
used with care for two reasons.
First Integer.parseInt
insists that the strings passed to it
contain only digits. No leading or trailing blanks are allowed.
Fortunately, there is a Java String
method
called trim that will remove these blanks.
The second problem is a bit worse. When parseInt
is passed the string "R2D2
" it will throw the
exception NumberFormatException.
To handle this problem
you must enclose your call to the parseInt
method
within a try
block.
We are also going to ask you to write the sum of adding
all of these integers to the file iolabsum.txt.
To do this you must use the constructor for
FileOutputStream to open iolabsum.txt
as an OutputStream.
Then the OutputStream
may be used to
create a PrintStream. Fortunately, println
is a
method of PrintStream
. By the way,
System.out
is also a PrintStream
, so you already know how to use one.
Your first task is to add up all the numbers in iolabin.txt
and print out their sum to System.out
.
Your second, and final, task is to create a file called iolabsum.txt and print the sum to that file.