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
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.