Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 17 – Files and Streams

Similar presentations


Presentation on theme: "Chapter 17 – Files and Streams"— Presentation transcript:

1 Chapter 17 – Files and Streams
Outline Data Hierarchy Files and Streams Class File Creating a Sequential-Access File Reading Data from a Sequential-Access File Updating Sequential-Access Files Random-Access Files Creating a Random-Access File Writing Data Randomly to a Random-Access File Reading Data Sequentially from a Random-Access File New I/O APIs for the Java Platform

2 File is a group of related records
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

3 Java views a file as a stream of bytes (Fig. 17.2)
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

4 Fig. 17.2 Java’s view of a file of n bytes
3 ... 1 2 4 5 8 9 n-1 end-of-file marker 6 7

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

6 17.4 Class File Class File Constructors
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

7 Fig File methods

8 FileTest.java Line 5 Import java.io package
// Fig. 17.4: FileTest.java // Demonstrating the File class. import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; 7 public class FileTest extends JFrame implements ActionListener { 10 private JTextField enterField; private JTextArea outputArea; 13 // set up GUI public FileTest() { super( "Testing class File" ); 18 enterField = new JTextField( "Enter file or directory name here" ); enterField.addActionListener( this ); outputArea = new JTextArea(); 22 ScrollPane scrollPane = new ScrollPane(); scrollPane.add( outputArea ); 25 FileTest.java Line 5 Import java.io package

9 FileTest.java Line 38 Line 41
Container container = getContentPane(); container.add( enterField, BorderLayout.NORTH ); container.add( scrollPane, BorderLayout.CENTER ); 29 setSize( 400, 400 ); setVisible( true ); 32 } // end constructor 34 // display information about file user specifies public void actionPerformed( ActionEvent actionEvent ) { File name = new File( actionEvent.getActionCommand() ); 39 // if name exists, output information about it if ( name.exists() ) { outputArea.setText( name.getName() + " exists\n" + ( name.isFile() ? "is a file\n" : "is not a file\n" ) + ( name.isDirectory() ? "is a directory\n" : "is not a directory\n" ) + ( name.isAbsolute() ? "is absolute path\n" : "is not absolute path\n" ) + "Last modified: " + name.lastModified() + "\nLength: " + name.length() + "\nPath: " + name.getPath() + "\nAbsolute path: " + name.getAbsolutePath() + "\nParent: " + name.getParent() ); 51 FileTest.java Line 38 Line 41 create a new File and assign it to name Body of if outputs information about the file if it exists

10 FileTest.java Line 53 Lines 57-58 Lines 63-64
// output information if name is a file if ( name.isFile() ) { 54 // append contents of file to outputArea try { BufferedReader input = new BufferedReader( new FileReader( name ) ); StringBuffer buffer = new StringBuffer(); String text; outputArea.append( "\n\n" ); 62 while ( ( text = input.readLine() ) != null ) buffer.append( text + "\n" ); 65 outputArea.append( buffer.toString() ); } 68 // process file processing problems catch ( IOException ioException ) { JOptionPane.showMessageDialog( this, "FILE ERROR", "FILE ERROR", JOptionPane.ERROR_MESSAGE ); } 74 } // end if 76 Test if our object is a file FileTest.java Line 53 Lines Lines 63-64 Create reader to gather data from the file Read text until there is no more in the file

11 FileTest.java Line 79 Lines 91-93
// output directory listing else if ( name.isDirectory() ) { String directory[] = name.list(); 80 outputArea.append( "\n\nDirectory contents:\n"); 82 for ( int i = 0; i < directory.length; i++ ) outputArea.append( directory[ i ] + "\n" ); } 86 } // end outer if 88 // not file or directory, output error message else { JOptionPane.showMessageDialog( this, actionEvent.getActionCommand() + " Does Not Exist", "ERROR", JOptionPane.ERROR_MESSAGE ); } 95 } // end method actionPerformed 97 public static void main( String args[] ) { FileTest application = new FileTest(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } 103 104 } // end class FileTest Get a list of the files in the directory FileTest.java Line 79 Lines 91-93 If file does not exist, display error

12 FileTest.java

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

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);

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);

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);

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

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);

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); } ...

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

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

22 “Instant-access” applications
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

23 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 are valid account numbers

24 17.8-17.11 Random-Access Files import java.io.*; ...
RandomAccessFile randy; try { randy = new RandomAccessFile ("accounts.txt", "rw"); } catch (IOException ioe) {}

25 17.8-17.11 Random-Access Files try { if ((account >= 1) &&
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) {} ...

26 17.8-17.11 Random-Access Files try { if ((account >= 1) &&
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();

27 17.8-17.11 Random-Access Files catch (EOFException eof) {
randy.seek(0); } catch (IOException ioe) {} ... try randy.close();

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

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

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


Download ppt "Chapter 17 – Files and Streams"

Similar presentations


Ads by Google