26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 16 Files and Streams 967<br />

length (line 55), getPath (line 56), getAbsolutePath (line 57) and getParent<br />

(line 58).<br />

If the File object represents a file (line 61), the program reads the contents of the file<br />

and displays the contents in the JTextArea. The program uses a BufferedReader<br />

object chained <strong>to</strong> a FileReader object (lines 65–66) <strong>to</strong> open the file for reading and <strong>to</strong><br />

read the file one line at a time with method readLine (line 71). Note that the<br />

FileReader object was initialized with the File object name (line 66).<br />

If the File object represents a direc<strong>to</strong>ry (line 86), the program reads the contents of<br />

the direc<strong>to</strong>ry in<strong>to</strong> the program by using File method list, then displays the direc<strong>to</strong>ry<br />

contents in the JTextArea.<br />

The first output demonstrates a File object associated with the jfc direc<strong>to</strong>ry from<br />

the <strong>Java</strong> 2 Software Development Kit. The second output of this program demonstrates a<br />

File object associated with the readme.txt file from the <strong>Java</strong> 2 Software Development<br />

Kit. In both cases, we specified an absolute path on our personal computer.<br />

Note that the \ separa<strong>to</strong>r character is used <strong>to</strong> separate direc<strong>to</strong>ries and files in the path.<br />

On a UNIX workstation, the separa<strong>to</strong>r character would be the / character. <strong>Java</strong> processes<br />

both characters identically in a path name. So, if we specified the path<br />

c:\java/readme.txt<br />

which uses one of each separa<strong>to</strong>r character, <strong>Java</strong> still processes the file properly.<br />

Common <strong>Program</strong>ming Error 16.2<br />

Using \ as a direc<strong>to</strong>ry separa<strong>to</strong>r rather than \\ in a string literal is a logic error. A single<br />

\ indicates that the \ and the next character represent an escape sequence. To insert a \ in<br />

a string literal, you must use \\. 16.2<br />

SUMMARY<br />

Good <strong>Program</strong>ming Practice 16.6<br />

When building Strings that represent path information, use File.separa<strong>to</strong>rChar <strong>to</strong><br />

obtain the local computer’s proper separa<strong>to</strong>r character, rather than explicitly using / or \. 16.6<br />

• All data items processed by a computer are reduced <strong>to</strong> combinations of zeros and ones.<br />

• The smallest data item in a computer (a bit) can assume the value 0 or the value 1.<br />

• Digits, letters and special symbols are called characters. The set of all characters used <strong>to</strong> write programs<br />

and represent data items on a particular computer is called that computer’s character set.<br />

Every character in a computer’s character set is represented as a pattern of 1s and 0s. (Characters<br />

in <strong>Java</strong> are Unicode characters composed of 2 bytes.)<br />

• A field is a group of characters (or bytes) that conveys meaning.<br />

• A record is a group of related fields.<br />

• At least one field in a record is chosen as a record key <strong>to</strong> identify a record as belonging <strong>to</strong> a particular<br />

person or entity that is unique from all other records in the file.<br />

• <strong>Java</strong> imposes no structure on a file. Notions like “record” do not exist in <strong>Java</strong>. The programmer<br />

must structure a file appropriately <strong>to</strong> meet the requirements of an application.<br />

• A collection of programs designed <strong>to</strong> create and manage databases is called a database management<br />

system (DBMS).<br />

• <strong>Java</strong> views each file as a sequential stream of bytes.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!