Chapter 17 – Files and Streams

Slides:



Advertisements
Similar presentations
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. 2 Operating System Application #1 Application #2 Java Virtual Machine #1 Local Memory Shared Memory Threads.
Advertisements

1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
1 Streams and Input/Output Files Part 3. 2 Handling Primitive Data Types The basic input and output streams provide read/write methods that can be used.
Lecture 15: I/O and Parsing
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
Files Files are used to store long term data. –Typically referred to as persistent data. –A file is a group of related records. –A list of friends addresses.
Chapter 91 Streams and File I/O Chapter 9. 2 Announcements Project 5 due last night Project 6 assigned Exam 2 –Wed., March 21, 7:00 – 8:00 pm, LILY 1105.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 – Files and Streams Outline 17.1 Introduction 17.2 Data Hierarchy 17.3 Files and Streams 17.4.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
CIS 270—Application Development II Chapter 14—Files and Streams.
Introduction Files –Long-term storage of large amounts of data –Persistent data exists after termination of program –Files stored on secondary storage.
1 Chapter 11 – Files and Streams 1 Introduction What are files? –Long-term storage of large amounts of data –Persistent data exists after termination of.
Input / Output Chapter 13.  We use files all the time  Programs are files  Documents are files  We want them to be “permanent”  To last beyond execution.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Files and Streams. Midterm exam Time: Wednesday, October 31, 2007 Format: Multiple choices (about 15 to 20 questions) Determine the results of the code.
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Binary Files, Random Access Files Binary Files The way data is stored in memory is sometimes called the raw binary format. Data can be stored in.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Files and Streams Chapter What You Will Learn Create files Read files Write files Update files.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
Introduction to Computation and Problem Solving Class 29: Introduction to Streams Prof. Steven R. Lerman and Dr. V. Judson Harward.
Streams and File I/O Chapter 9. Outline Overview of Streams and File I/O Text-File I/O Using the File Class Basic Binary-File I/O Object I/O with Object.
 Pearson Education, Inc. All rights reserved Files and Streams.
1 Chapter 19 Binary I/O. 2 Motivations F Data stored in a text file – is represented in human-readable form –Text file –Readable –Java source programs.
1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling.
Chapter 15: Input and Output
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
Java Programming, Second Edition Chapter Sixteen File Input and Output.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Java IO Exploring the java.io package and living to talk about it.
The Java IO System Different kinds of IO Different kinds of operations
IO in java.
IO in java.
Input and Output Streams – part I
Chapter 14 – Exception Handling
Ch14 Files and Streams OBJECTIVES
“Form Ever Follows Function” Louis Henri Sullivan
Chapter 17 Binary I/O.
Object Writing in files
I/O Basics.
Chapter 17 Binary I/O 1.
Accessing Files in Java
Chapter 13: File Input and Output
Chapter 17 Binary I/O Dr. Clincy - Lecture.
MSIS 670: Object-Oriented Software Engineering
14 Files and Streams.
Files and Streams in Java
14 Files and Streams.
CS 240 – Advanced Programming Concepts
Chapter 15 Files, Streams and Object Serialization
Chapter 15 – Exception Handling
The Package Statement Group related interfaces and classes together
Java Chapter 2 (Estifanos Tilahun Mihret--Tech with Estif)
David Davenport Spring 2005
Presentation transcript:

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

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

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

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

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

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

Fig. 17.3 File methods

FileTest.java Line 5 Import java.io package 1 // Fig. 17.4: FileTest.java 2 // 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 JFrame 9 implements ActionListener { 10 11 private JTextField enterField; 12 private JTextArea outputArea; 13 14 // set up GUI 15 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 FileTest.java Line 5 Import java.io package

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 constructor 34 35 // display information about file user specifies 36 public void actionPerformed( ActionEvent actionEvent ) 37 { 38 File name = new File( actionEvent.getActionCommand() ); 39 40 // if name exists, output information about it 41 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 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

FileTest.java Line 53 Lines 57-58 Lines 63-64 52 // output information if name is a file 53 if ( name.isFile() ) { 54 55 // append contents of file to outputArea 56 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 problems 70 catch ( IOException ioException ) { 71 JOptionPane.showMessageDialog( this, "FILE ERROR", 72 "FILE ERROR", JOptionPane.ERROR_MESSAGE ); 73 } 74 75 } // end if 76 Test if our object is a file FileTest.java Line 53 Lines 57-58 Lines 63-64 Create reader to gather data from the file Read text until there is no more in the file

FileTest.java Line 79 Lines 91-93 77 // output directory listing 78 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 if 88 89 // not file or directory, output error message 90 else { 91 JOptionPane.showMessageDialog( this, 92 actionEvent.getActionCommand() + " Does Not Exist", 93 "ERROR", JOptionPane.ERROR_MESSAGE ); 94 } 95 96 } // end method actionPerformed 97 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 Get a list of the files in the directory FileTest.java Line 79 Lines 91-93 If file does not exist, display error

FileTest.java

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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