2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 – Files and Streams Outline 17.2 Data Hierarchy 17.3 Files and Streams 17.4 Class File 17.5 Creating a Sequential-Access File 17.6 Reading Data from a Sequential-Access File 17.7 Updating Sequential-Access Files 17.8 Random-Access Files 17.9 Creating a Random-Access File 17.10 Writing Data Randomly to a Random-Access File 17.11 Reading Data Sequentially from a Random- Access File 17.13 New I/O APIs for the Java Platform
Chapter 17 – Files and Streams. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
2003 Prentice Hall, Inc. All rights reserved.
1
Chapter 17 – Files and StreamsOutline17.2 Data Hierarchy17.3 Files and Streams17.4 Class File17.5 Creating a Sequential-Access File17.6 Reading Data from a Sequential-Access File17.7 Updating Sequential-Access Files17.8 Random-Access Files17.9 Creating a Random-Access File17.10 Writing Data Randomly to a Random-Access File17.11 Reading Data Sequentially from a Random-Access File17.13 New I/O APIs for the Java Platform
2003 Prentice Hall, Inc. All rights reserved.
2
17.2 Data Hierarchy
• Records– Composed of several related fields
– Implemented as a class in Java
• File is a group of related records– One field in each record is a record key
• Record key is a unique identifier for a record
– Sequential file• Records stored in order by record key
• Database is group of related files
2003 Prentice Hall, Inc. All rights reserved.
3
17.3 Files and Streams
• Java views a file as a stream of bytes (Fig. 17.2)– File ends with end-of-file marker
– File as a stream of bytes associated with an object• Java also associates streams with devices
– System.in, System.out, and System.err– Streams can be redirected
• File processing with classes in package java.io– FileInputStream– DataInputStream– FileOutputStream– DataOutputStream
2003 Prentice Hall, Inc. All rights reserved.
4
Fig. 17.2 Java’s view of a file of n bytes
0 3
...
1 2 4 5 8 9...
n-1
end-of-file marker
6 7
2003 Prentice Hall, Inc. All rights reserved.
5
17.3 Files and Streams
• Buffering– Improves performance of I/O
– Copies each output to a region of memory called a buffer
– Entire buffer output to disk at once• One long disk access takes less time than many smaller ones
– BufferedOutputStream buffers file output
– BufferedInputStream buffers file input
2003 Prentice Hall, Inc. All rights reserved.
6
17.4 Class File
• Class File– Provides useful information about a file or directory
– Does not open files or process files
• Constructors– File (String name) can contain path information or
just be file name
– File (String path, String name)– File (File directory, String name)– File (URI uri) can be URL or something like file:/C:/employee.txt
2003 Prentice Hall, Inc. All rights reserved.
7
Fig. 17.3 File methodsMethod Description boolean canRead() Returns true if a file is readable; false otherwise.
boolean canWrite() Returns true if a file is writable; false otherwise.
boolean exists() Returns true if the name specified as the argument to the File constructor is a file or directory in the specified path; false otherwise.
boolean isFile() Returns true if the name specified as the argument to the File constructor is a file; false otherwise.
boolean isDirectory() Returns true if the name specified as the argument to the File constructor is a directory; false otherwise.
boolean isAbsolute() Returns true if the arguments specified to the File constructor indicate an absolute path to a file or directory; false otherwise.
String getAbsolutePath() Returns a string with the absolute path of the file or directory.
String getName() Returns a string with the name of the file or directory.
String getPath() Returns a string with the path of the file or directory.
String getParent() Returns a string with the parent directory of the file or directory—that is, the directory in which the file or directory can be found.
long length() Returns the length of the file, in bytes. If the File object represents a directory, 0 is returned.
long lastModified() Returns a platform-dependent representation of the time at which the file or directory was last modified. The value returned is useful only for comparison with other values returned by this method.
String[] list() Returns an array of strings representing the contents of a directory. Returns null if the File object is not a directory.
35 // display information about file user specifies36 public void actionPerformed( ActionEvent actionEvent )37 {38 File name = new File( actionEvent.getActionCommand() );39
40 // if name exists, output information about it41 if ( name.exists() ) {42 outputArea.setText( name.getName() + " exists\n" + 43 ( name.isFile() ? "is a file\n" : "is not a file\n" ) +44 ( name.isDirectory() ? "is a directory\n" :45 "is not a directory\n" ) +46 ( name.isAbsolute() ? "is absolute path\n" : 47 "is not absolute path\n" ) + "Last modified: " + 48 name.lastModified() + "\nLength: " + name.length() +49 "\nPath: " + name.getPath() + "\nAbsolute path: " + 50 name.getAbsolutePath() + "\nParent: " + name.getParent() );51
create a new File and assign it to name
Body of if outputs information about the
file if it exists
2003 Prentice Hall, Inc.All rights reserved.
Outline10
FileTest.java
Line 53
Lines 57-58
Lines 63-64
52 // output information if name is a file53 if ( name.isFile() ) {54
55 // append contents of file to outputArea56 try {57 BufferedReader input = new BufferedReader(58 new FileReader( name ) ); 59 StringBuffer buffer = new StringBuffer();60 String text;61 outputArea.append( "\n\n" );62 63 while ( ( text = input.readLine() ) != null ) 64 buffer.append( text + "\n" );65 66 outputArea.append( buffer.toString() );67 }68