0% found this document useful (0 votes)
48 views

Java How To Program, 10/e Late Objects Version: Reserved

Uploaded by

Maram Alanzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Java How To Program, 10/e Late Objects Version: Reserved

Uploaded by

Maram Alanzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Java How to Program, 10/e

Late Objects Version

© Copyright 1992-2015 by Pearson Education, Inc. All Rights


Reserved.
 The content is mainly selected (sometimes modified)
from the original slides provided by the authors of the
textbook

 Readings
 Chapter 15: Files, Streams and Object Serialization

©1992-2015 by Pearson Education, Inc.


All Rights Reserved.
15.1  Introduction
15.2  Files and Streams
15.4  Sequential-Access Text Files
15.4.1 Creating a Sequential-Access Text File
15.4.2 Reading Data from a Sequential-Access Text File
15.4.4 Updating Sequential-Access Files
Optional:
15.3  Using NIO Classes and Interfaces to Get File and
Directory Information

©1992-2015 by Pearson Education, Inc.


All Rights Reserved.
 Data stored in variables and arrays is temporary
 It’s lost when a local variable goes out of scope or when the
program terminates
 For long-term retention of data, computers use files.
 Computers store files on secondary storage devices

 hard disks, flash drives, DVDs and more.


 Data maintained in files is persistent data because it
exists beyond the duration of program execution.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Java views each file as a sequential stream of bytes
(Fig. 15.1).
 Every operating system provides a mechanism to

determine the end of a file, such as an end-of-file


marker or a count of the total bytes in the file that is
recorded in a system-maintained administrative data
structure.
 A Java program simply receives an indication from the

operating system when it reaches the end of the stream

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 File streams can be used to input and output data as bytes or
characters.
 Byte-based streams output and input data in its binary format—a
char is two bytes, an int is four bytes, a double is eight bytes,
etc.
 Character-based streams output and input data as a sequence of
characters in which every character is two bytes—the number of
bytes for a given value depends on the number of characters in that
value.
 Files created using byte-based streams are referred to as
binary files.
 Files created using character-based streams are referred to as
text files. Text files can be read by text editors.
 Binary files are read by programs that understand the
specific content of the file and the ordering of that content.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 A Java program opens a file by creating an object and
associating a stream of bytes or characters with it.
 Can also associate streams with different devices.
 Java creates three stream objects when a program begins
executing
 System.in (standard input stream) object normally inputs bytes
from the keyboard
 Object System.out (the standard output stream object) normally
outputs character data to the screen
 Object System.err (the standard error stream object) normally
outputs character-based error messages to the screen.
 Class System provides methods setIn, setOut and
setErr to redirect the standard input, output and error
streams, respectively.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Java programs perform file processing by using classes
from package java.io and the subpackages of
java.nio.
 Character-based input and output can be performed

with classes Scanner and Formatter.


 Class Scanner is used extensively to input data from the
keyboard. This class can also read data from a file.
 Class Formatter enables formatted data to be output to any
text-based stream in a manner similar to method
System.out.printf.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Sequential-access files store records in order by the
record-key field.
 Text files are human-readable files.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Java imposes no structure on a file
 Notions such as records do not exist as part of the Java
language.
 You must structure files to meet the requirements of your
applications.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Formatter outputs formatted Strings to the
specified stream.
 The constructor with one String argument receives

the name of the file, including its path.


 If a path is not specified, the JVM assumes that the file is in
the directory from which the program was executed.
 If the file does not exist, it will be created.
 If an existing file is opened, its contents are truncated.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 An Exception occurs when there are an error while
executing the program:
 if the user does not have permission to write data to the file.
 if the file does not exist and a new file cannot be created.
 if the data being read by a Scanner method is in the
wrong format or if there is no more data to input.
 if the Formatter is closed when you attempt to output

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Scanner method hasNext determines whether the end-
of-file key combination has been entered.
 Formatter method format works like
System.out.printf
 Formatter method close closes the file.
 If method close is not called explicitly, the operating system
normally will close the file when program execution terminates.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 The application (Fig. 15.6) reads records from the file
"clients.txt" created by the application of
Section 15.4.1 and displays the record contents.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 The data in many sequential files cannot be modified
without the risk of destroying other data in the file.
 If the name “White” needed to be changed to
“Worthington,” the old name cannot simply be
overwritten, because the new name requires more space.
 Fields in a text file—and hence records—can vary in size.
 Records in a sequential-access file are not usually updated
in place. Instead, the entire file is rewritten.
 Rewriting the entire file is uneconomical to update just one
record, but reasonable if a substantial number of records
need to be updated.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Interfaces Path and DirectoryStream and classes
Paths and Files (all from package
java.nio.file) are useful for retrieving
information about files and directories on disk:
 Path interface—Objects of classes that implement this
interface represent the location of a file or directory. Path
objects do not open files or provide any file-processing
capabilities.
 Paths class—Provides static methods used to get a Path
object representing a file or directory location.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Files class—Provides static methods for common file
and directory manipulations, such as copying files; creating
and deleting files and directories; getting information about
files and directories; reading the contents of files; getting
objects that allow you to manipulate the contents of files and
directories; and more
 DirectoryStream interface—Objects of classes that
implement this interface enable a program to iterate through
the contents of a directory.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 A file or directory’s path specifies its location on disk.
The path includes some or all of the directories leading
to the file or directory.
 An absolute path contains all directories, starting with
the root directory, that lead to a specific file or
directory.
 Every file or directory on a particular disk drive has the
same root directory in its path.
 A relative path is “relative” to another directory—for
example, a path relative to the directory in which the
application began executing.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 An overloaded version of Files static method get uses a URI
object to locate the file or directory.
 A Uniform Resource Identifier (URI) is a more general
form of the Uniform Resource Locators (URLs) that are
used to locate websites.
 On Windows platforms, the URI
 file://C:/data.txt
 identifies the file data.txt stored in the root directory of
the C: drive. On UNIX/Linux platforms, the URI
 file:/home/student/data.txt
 identifies the file data.txt stored in the home directory of
the user student.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Figure 15.2 prompts the user to enter a file or directory name,
then uses classes Paths, Path, Files and
DirectoryStream to output information about that file or
directory.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 A separator character is used to separate directories
and files in a path.
 On a Windows computer, the separator character is a
backslash (\).
 On a Linux or Mac OS X system, it’s a forward slash (/).
 Java processes both characters identically in a path
name.
 For example, if we were to use the path
 c:\Program Files\Java\jdk1.6.0_11\demo/jfc
 which employs each separator character, Java would
still process the path properly.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.

You might also like