CS 1302 – Lab 06
This lab provides an introduction to reading and writing text files. There are 6 stages to complete this lab:
Stage
Title
Text Reference
1
Reading Simply Formatted Data from a Text File
12.11
2
Parsing Data
12.11
3
More Parsing
12.11
4
Writing Data to a Text File
12.11
5
The
File
Class
12.10
6
Reading & Writing Example
12.11
To make this document easier to read, it is recommended that you turn off spell checking in Word:
1.
Choose: File, Option, Proofing
2.
At the very bottom, check: “Hide spelling errors…” and “Hide grammar errors…”
Stage 1 -
Reading Simply Formatted Data from a Text File
In this stage you will learn how to read an arbitrary number of items with a repetitive pattern from a text file.
1.
Read (no action required)
–
a.
A
text file
is actually a file filled with binary digits, 1’s and 0’s (which
itself is an abstraction). However, the software (Word, Eclipse,
etc.
)
that accesses a text file uses
character decoding.
For example, the
binary stream:
01001010011000010111011001100001
Is decoded (each 8 bits) as:
8-bit Blocks
0100101
0
0110000
1
0111011
0
0110000
1
Decoded ASCII Text
J
a
v
a
b.
The encoding/decoding scheme shown on the right is
ASCII
. For example the (partial) table above on the
right shows how text is encoded/decoded. For example, the ASCII letter “a” is represented in binary as:
01100001. ASCII was the standard on the internet until 2007. Now,
UTF-8
is the standard. UTF-8 was
designed to be backwards compatible with ASCII, so they are the same. Be default, Java reads and writes
using ASCII, however you can set a parameter to use UTF-8.
c.
The information above is not directly needed for this course but is useful for a general understanding.
Usually (and always for this course) when we read/write information to/from a text file the
decoding/encoding is done automatically.
1
ASCII
Dec
Hex
Binary
0
48
30
0011 0000
1
49
31
0011 0001
2
50
32
0011 0010
…
…
…
…
A
65
41
0100 0001
B
66
42
0100 0010
C
67
43
0100 0011
…
…
…
…
a
97
61
0110 0001
b
98
62
0110 0010
c
99
63
0110 0011
…
…
…
…

2.Read (no action required)– a.Consider a text file with the data shown on the right. The line numbers (1-15) are shownon the left and are not a part of the file. Each three lines represents an employeename, salary, age. b.To read (or write) data we first create an instance of the File class which contains thelocation of the file. Later in this tutorial we will look more closely at the File class. Fornow, we will just see how to create an instance. The File class constructor accepts a pathwith the location and name of the file. For example: File inFile = newFile( "src\\examples1\\employees1.txt" specifies that the file is named employee1.txt and it is found in the src\examples1 folder.An additional “\” is needed to delimit the single, required “\” in the path. Notice thatthis is a relative path. Eclipse is configured to look for a file in the src folder by defaultand since we will typically have our code in a package (example1 in the example above)we need to specify the full path.c.We use the Scannerclass in Java to read from a text file (just as you did to read from the keyboard) passinginto its constructor a File object.Scanner input = newScanner( inFile d.
:
);
);
