chapter 17 – files and streams

30
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

Upload: nen

Post on 08-Jan-2016

43 views

Category:

Documents


2 download

DESCRIPTION

Chapter 17 – Files and Streams. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 17 – Files and Streams

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

Page 2: Chapter 17 – Files and Streams

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

Page 3: Chapter 17 – Files and Streams

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

Page 4: Chapter 17 – Files and Streams

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

Page 5: Chapter 17 – Files and Streams

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

Page 6: Chapter 17 – Files and Streams

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

Page 7: Chapter 17 – Files and Streams

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.

Page 8: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc.All rights reserved.

Outline8

FileTest.java

Line 5

1 // Fig. 17.4: FileTest.java2 // Demonstrating the File class.3 import java.awt.*;4 import java.awt.event.*;5 import java.io.*;6 import javax.swing.*;7

8 public class FileTest extends JFrame9 implements ActionListener {10

11 private JTextField enterField;12 private JTextArea outputArea;13 14 // set up GUI15 public FileTest()16 {17 super( "Testing class File" );18

19 enterField = new JTextField( "Enter file or directory name here" );20 enterField.addActionListener( this );21 outputArea = new JTextArea();22

23 ScrollPane scrollPane = new ScrollPane();24 scrollPane.add( outputArea );25

Import java.io package

Page 9: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc.All rights reserved.

Outline9

FileTest.java

Line 38

Line 41

26 Container container = getContentPane();27 container.add( enterField, BorderLayout.NORTH );28 container.add( scrollPane, BorderLayout.CENTER );29

30 setSize( 400, 400 );31 setVisible( true );32

33 } // end constructor34

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

Page 10: Chapter 17 – Files and Streams

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

69 // process file processing problems70 catch ( IOException ioException ) {71 JOptionPane.showMessageDialog( this, "FILE ERROR",72 "FILE ERROR", JOptionPane.ERROR_MESSAGE );73 }74

75 } // end if76

Test if our object is a file

Create reader to gather data from the file

Read text until there is no more in the file

Page 11: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc.All rights reserved.

Outline11

FileTest.java

Line 79

Lines 91-93

77 // output directory listing78 else if ( name.isDirectory() ) {79 String directory[] = name.list();80 81 outputArea.append( "\n\nDirectory contents:\n");82 83 for ( int i = 0; i < directory.length; i++ )84 outputArea.append( directory[ i ] + "\n" );85 }86

87 } // end outer if88

89 // not file or directory, output error message90 else {91 JOptionPane.showMessageDialog( this,92 actionEvent.getActionCommand() + " Does Not Exist",93 "ERROR", JOptionPane.ERROR_MESSAGE );94 } 95

96 } // end method actionPerformed97

98 public static void main( String args[] )99 {100 FileTest application = new FileTest();101 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );102 }103

104 } // end class FileTest

If file does not exist, display error

Get a list of the files in the directory

Page 12: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc.All rights reserved.

Outline12

FileTest.java

Page 13: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

13

17.5 Creating a Sequential-Access File

• Simpler example than in book– Uses DataOutputStream instead of ObjectOutputStream

– DataOutputStream is binary representation of data

– Use Writer object (such as BufferedWriter) to write character stream

– DataOutputStream (writes data via writeDouble, writeFloat, writeInt, writeUTF, etc.)

– writeInt writes 4 byte integer

– writeUTF writes 2 bytes that tell how many bytes follow

Page 14: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

14

17.5 Creating a Sequential-Access File

import java.io.*; ... DataOutputStream output; ... try { output = new DataOutputStream (new FileOutputStream ("accounts.txt")); } catch (IOException ioe) { System.err.println ( "File did not open properly" + ioe.toString()); System.exit (1); }

Page 15: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

15

17.5 Creating a Sequential-Access File

... try { output.writeInt(int); output.writeUTF(String); output.writeFloat(float);

} catch (IOException ioe) { System.err.println ( "Error writing to file" + ioe.toString()); System.exit (1); } ...

Page 16: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

16

17.5 Creating a Sequential-Access File

try { output.close(); } catch (IOException ioe) { System.err.println ( "File did not close properly" + ioe.toString());

System.exit (1); }

Page 17: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

17

17.6 Reading Data from a Sequential-Access File

• Simpler example than in book– Uses DataInputStream instead of ObjectInputStream

– DataInputStream is binary representation of data

– Use Reader object (such as BufferedReader) to read character stream

– DataInputStream (reads data via readDouble, readFloat, readInt, readUTF (characters), etc.)

– readInt reads 4 byte integer

– readUTF reads 2 bytes that tell how many bytes follow

• Accessing a sequential file– Data must be read in same format it was written

Page 18: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

18

17.6 Reading Data from a Sequential-Access File

import java.io.*; ... DataInputStream input; ... try { input = new DataInputStream (new FileInputStream ("accounts.txt")); } catch (IOException ioe) { System.err.println ( "File did not open properly" + ioe.toString()); System.exit (1); } ...

Page 19: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

19

17.6 Reading Data from a Sequential-Access File

try { int=input.readInt(); String=input.readUTF(); float=input.readFloat(); } catch (EOFException eof) { closeFile(); } catch (IOException ioe) { System.err.println ( "Error reading from file" + ioe.toString()); System.exit (1); } ...

Page 20: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

20

17.6 Reading Data from a Sequential-Access File

try { input.close(); } catch (IOException ioe) { System.err.println ( "File did not close properly" + ioe.toString());

System.exit (1); }

Attempt to read past end-of-file marker throws EOFException

Page 21: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

21

17.7 Updating Sequential-Access Files

• Difficult to update a sequential-access file– Entire file must be rewritten to change one field

– Only acceptable if many records being updated at once

Page 22: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

22

17.8-17.11 Random-Access Files

• “Instant-access” applications– Record must be located immediately

– Transaction-processing systems require rapid access

• Random-access files– Access individual records directly and quickly

– Use fixed length for every record• Easy to calculate record locations

– Insert records without destroying other data in file

Page 23: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

23

17.8-17.11 Random-Access Files

– RandomAccessFile objects have all capabilities of DataInputStream and DataOutputStream objects

– data read/written at location specified by file pointer

– always read data in exactly same way as it was written

– seek(n), re-position file pointer to byte n of the file, n must be long integer

– In code below assume that accounts.txt contains records of int account (4 bytes), String name (12 2-byte characters), float balance (4 bytes)

– Each record is 32 bytes

– Assume accounts 1-1000 are valid account numbers

Page 24: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

24

17.8-17.11 Random-Access Files

import java.io.*; ... RandomAccessFile randy; ... try { randy = new RandomAccessFile ("accounts.txt", "rw"); } catch (IOException ioe) {} ...

Page 25: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

25

17.8-17.11 Random-Access Files

try { if ((account >= 1) && (account <= 1000)) { randy.seek((long)(account-1)*32)); randy.writeInt(account); StringBuffer buf; buf = new StringBuffer (name); buf.setLength(12); randy.writeChars(buf.toString()); randy.writeFloat(balance); } } catch (IOException ioe) {} ...

Page 26: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

26

17.8-17.11 Random-Access Files

try { if ((account >= 1) && (account <= 1000)) { randy.seek((long)(account-1)*32)); account=randy.readInt(); char stuff[] = new char [12]; for (i=0; i<12; i++) { stuff[i]=randy.readChar(); } name=new String (stuff); balance=randy.readFloat(); } }

Page 27: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

27

17.8-17.11 Random-Access Files

catch (EOFException eof) { randy.seek(0); } catch (IOException ioe) {} ... try { randy.close(); } catch (IOException ioe) {}

Page 28: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

28

17.13 New I/O APIs for the Java Platform

• New in Java 1.4 (java.nio.*)• Buffers

– Consolidate I/O operations

– Four properties• Capacity (how much it can hold)

• Limit (current end)

• Position (next element to be read or written)

• Mark (remembered point)

– Put (write) and get (read) operations

Page 29: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

29

17.13 New I/O APIs for the Java Platform

• Channels– Interface Channel is a connection to an I/O device

• Interacts efficiently with buffers

– ReadableByteChannel interface• Method read

– WriteableByteChannel interface• Method write

Page 30: Chapter 17 – Files and Streams

2003 Prentice Hall, Inc. All rights reserved.

30

17.13 New I/O APIs for the Java Platform

• File Locks– Restricts access to a portion of a file

– FileChannel, position, size

– Exclusive or shared