CS 1054 Introduction to Programming in Java (Spring 2008)
Lab 10: File I/O - Text files.
Lab Objectives
- Learn how to read a text file using the Scanner class
- Learn how to write a file using the PrintWriter class
Instructions
- There will be no webcat submission for this week's lab.
The TA has to look through your code.
- Begin by downloading
Lab10.zip,
save the file, and then extract it in a folder you can locate.
Open this project in BlueJ.
Make sure TextFileReader, TextFileWriter, and URLReader are compiled.
- Study TextFileReader, execute it, and make sense of how it reads a file.
Locate the Test.txt file in the directory of the BlueJ project.
Open it using a text editor such as Notepad and modify its contents.
Save it and run TextFileReader again to verify that the output changes.
- Try to display TextFileReader.java.
Remember that the programs you write are also text files!
- Next, study TextFileWriter, execute it,
and make sense of how it reads from keyboard input
and write what you input to a file.
Locate the Output.txt in the directory and verify that the input was saved
correctly.
Experiment with saving input to a different file.
- The next step is to complete the TextFileCopy class.
It has to read the contents of a text file line by line and save
them to a different file.
In order to complete it you must use portions of the other two classes given.
Make sure you create two files, a Scanner and a PrintWriter.
Once you are done show your code to the TA and demonstrate that it works.
- Finally look at the URLReader class.
It outputs the HTML code for a given website URL.
Make sure you are using unlimited buffering as some codes can be very long.
To verify that the output is correct you can open the webpage in a Firefox
then press +U to display the HTML. Try outputting different webpages.