Introduction Files –Long-term storage of large amounts of data –Persistent data exists after termination of program –Files stored on secondary storage.

Slides:



Advertisements
Similar presentations
Lecture 15: I/O and Parsing
Advertisements

The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Files CSC 171 FALL 2001 LECTURE 18. History Operating Systems Operating systems (originally called monitors or supervisors) had been developed in the.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
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.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
© The McGraw-Hill Companies, 2006 Working with files Chapter 20.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Chapter 20 – Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.
Io package as Java’s basic I/O system continue’d.
Java I/O Input: information brought to program from an external source
1 Course Lectures Available on line:
Files and Streams 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
CIS 270—Application Development II Chapter 14—Files and Streams.
Stream: an object that either delivers data to its destination (screen, file, etc.) or that takes data from a source (keyboard, file, etc.) –it acts as.
Introduction Exception handling Exception Handles errors
CIS 260: App Dev I. 2 Objects and Reference Variables n Predefined Java classes you have used:  String —for defining and manipulating strings  Integer.
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.
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different input.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
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.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
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.
Examples for Project 2 From Deital and Deital, Java: How to Program, Third Edition Reusable and flexible UI Data Records with Sequential and Random Access.
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.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 11.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
 Pearson Education, Inc. All rights reserved Files and Streams.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
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.
 Learn about computer files  Use the Path class  Learn about  Streams  Buffers  file organization  Use Java’s IO classes to write to and read from.
Files and console applications Chapter 17 This chapter explains: What a file is How to read and write to files How to manipulate folder paths and file.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Ch14 Files and Streams OBJECTIVES
Chapter 11 and 19 in Horstmann 6th edition
I/O Basics.
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Chapter 17 – Files and Streams
MSIS 670: Object-Oriented Software Engineering
Files and Streams in Java
David Davenport Spring 2005
Presentation transcript:

Introduction Files –Long-term storage of large amounts of data –Persistent data exists after termination of program –Files stored on secondary storage devices Magnetic disks Optical disks Magnetic tapes –Sequential and random access files

Data Hierarchy Smallest data item in a computer is a bit –Bit can be either 0 or 1 –Bit short for “binary digit” Programmers work with higher level data items –Decimal digits: (0-9) –Letters: (A-Z and a-z) –Special symbols: (e.g., %, &, *, (, ), -, +, “, :, ?, /, etc.) –Java uses Unicode characters composed of 2 bytes A byte is normally 8 bits long Fields (Java instance variables) –Composed of characters or bytes –Conveys meaning

Data Hierarchy Data hierarchy –Data items in a computer form a hierarchy Progresses from bits, to characters, to fields, etc. Records –Composed of several fields –Implemented as a class in Java –See the figure on the next slide for example 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

The data hierarchy.

Files and Streams Java views a file as a stream of bytes –File ends with end-of-file marker or a specific byte number –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 for byte-based input from a file –FileOutputStream for byte-based output to a file –FileReader for character-based input from a file –FileWriter for character-based output to a file

Java’s view of a file of n bytes.

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 –A program can convert a unbuffered stream into a buffered stream using the wrapping idiom Example inputStream = new BufferedReader(new FileReader("xanadu.txt")); outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));

A portion of the class hierarchy of the java.io package.

A portion of the class hierarchy of the java.io package (cont.).

import java.util.StringTokenizer; import java.io.*; import java.text.DecimalFormat; class InventoryItem { private String name; private int units; // number of available units of this item private float price; // price per unit of this item private DecimalFormat fmt; public InventoryItem (String itemName, int numUnits, float cost) { name = itemName; units = numUnits; price = cost; fmt = new DecimalFormat ("0.##"); } public String toString() { return name + ":\t" + units + " at " + price + " = " + fmt.format ((units * price)); } Example: reading a text file

public class Inventory{ // Reads data about a store inventory from an input file, // creating an array of InventoryItem objects, then prints them. public static void main (String[] args) { final int MAX = 100; InventoryItem[] items = new InventoryItem[MAX]; StringTokenizer tokenizer; String line, name, file="inventory.txt"; int units, count = 0; float price; try{ FileReader fr = new FileReader (file); BufferedReader inFile = new BufferedReader (fr); line = inFile.readLine(); while (line != null) { tokenizer = new StringTokenizer (line); name = tokenizer.nextToken(); try { units = Integer.parseInt (tokenizer.nextToken()); price = Float.parseFloat (tokenizer.nextToken()); items[count++] = new InventoryItem (name, units, price); } catch (NumberFormatException exception) { System.out.println ("Error in input. Line ignored:"); System.out.println (line); } line = inFile.readLine(); } inFile.close(); Example: reading a text file

for (int scan = 0; scan < count; scan++) System.out.println (items[scan]); } catch (FileNotFoundException exception) { System.out.println ("The file " + file + " was not found."); } catch (IOException exception) { System.out.println (exception); } Example: reading a text file

import java.io.*; public class TestData { // Creates a file of test data that consists of ten lines each // containing ten integer values in the range 0 to 99. public static void main (String[] args) throws IOException { final int MAX = 10; int value; String file = "test.txt"; FileWriter fw = new FileWriter (file); BufferedWriter bw = new BufferedWriter (fw); PrintWriter outFile = new PrintWriter (bw); for (int line=1; line <= MAX; line++) { for (int num=1; num <= MAX; num++) { value = (int) (Math.random() * 100); outFile.print (value + " "); } outFile.println (); } outFile.close(); System.out.println ("Output file has been created: " + file); } Example: writing a text file

Data Streams and Object Streams Data streams support I/O of primitive data types Object streams support I/O of objects Most, but not all, standard classes support serialization of their objects. –Those classes implement the marker interface Serializable –Every instance variable of the class should is a Serializable Object stream classes –ObjectInputStream and ObjectOutputStream Output using the writeObject() method

Updating Sequential-Access File 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

Random-Access File “Instant-access” applications –Record must be located immediately –Transaction-processing systems require rapid access Random-access files –behave like a large array of bytes stored in the file system –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 –Figure on the next slide shows random-access file

Java’s view of a random-access file

Random-Access File RandomAccessFile objects –Like DataInputStream and DataOutputstream –Reads or writes data in spot specified by file-position pointer Manipulates all data as primitive types Normally writes one object at a time to file

// Record class for the RandomAccessFile programs. import java.io.*; public class Record { private int account; private String lastName; private String firstName; private double balance; public Record(int a, String f, String l, double b) { account=a; firstName=f; lastName=l; balance=b; } public Record() { account=0; firstName=null; lastName=null; balance=0; } // Read a record from the specified RandomAccessFile public void read( RandomAccessFile file ) throws IOException { account = file.readInt(); byte b1[] = new byte[ 15 ]; file.readFully( b1 ); firstName = new String( b1 ); firstName=firstName.trim(); byte b2[] = new byte[ 15 ]; file.readFully( b2 ); lastName = new String( b2 ); lastName=lastName.trim(); balance = file.readDouble(); } Defining a class to be used for reading and writing records to a random access file

// Write a record to the specified RandomAccessFile public void write( RandomAccessFile file ) throws IOException { file.writeInt( account ); byte b1[] = new byte[ 15 ]; if ( firstName != null ) b1 = firstName.getBytes(); file.write( b1 ); byte b2[] = new byte[ 15 ]; if ( lastName != null ) b2 = lastName.getBytes(); file.write( b2 ); file.writeDouble( balance ); } public int getAccount(){ return account;} public String getlName(){ return lastName;} public String getfName(){ return firstName;} public double getBalance(){ return balance;} // NOTE: This method contains a hard coded value for the size of a // record of information. public int size() { return 42; } }

import java.io.*; public class CreateRandFile { private Record blank; RandomAccessFile file; public CreateRandFile() { blank = new Record(); try { file = new RandomAccessFile( "credit.dat", "rw" ); } catch( IOException e ) { System.err.println( "File not opened properly\n" + e.toString() ); System.exit( 1 ); } public void create() { try { for ( int i = 0; i < 100; i++ ) blank.write( file ); } catch ( IOException e ) { System.err.println( e.toString() ); } public static void main( String args[] ) { CreateRandFile accounts = new CreateRandFile(); accounts.create(); } This program creates a random access file sequentially by writing 100 empty records to disk.

import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class WriteRandFile extends JFrame { // Application window components JTextField acct, // where user enters account number fName, // where user enters first name lName, // where user enters last name bal; // where user enters balance JButton enter, // send record to file done; // quit program JLabel acctLabel, // account label fNameLabel, // first name label lNameLabel, // last name label balLabel; // balance label RandomAccessFile output; // file for output Record data; public WriteRandFile() { super( "Write to random access file" ); data = new Record(); try { output = new RandomAccessFile( "credit.dat", "rw" ); } catch ( IOException e ) { System.err.println( e.toString() ); System.exit( 1 ); } This program uses TextFields to get information from the user at the keyboard and writes the information to a random access file.

Container c = getContentPane(); c.setLayout( new GridLayout( 5, 2 ) ); acct = new JTextField( 20 ); acctLabel = new JLabel( "Account Number" ); fName = new JTextField( 20 ); fNameLabel = new JLabel( "First Name" ); lName = new JTextField( 20 ); lNameLabel = new JLabel( "Last Name" ); bal = new JTextField( 20 ); balLabel = new JLabel( "Balance" ); enter = new JButton( "Enter" ); done = new JButton( "Done" ); c.add( acctLabel ); // add label c.add( acct ); // add TextField c.add( fNameLabel ); // add label c.add( fName ); // add TextField c.add( lNameLabel ); // add label c.add( lName ); // add TextField c.add( balLabel ); // add label c.add( bal ); // add TextField c.add( enter ); // add button c.add( done ); // add button done.addActionListener( new ActionListener () { public void actionPerformed( ActionEvent event ){ cleanup(); // write data, close file, etc. setVisible(false); dispose(); // release system resources System.exit( 0 ); } } ); This program uses TextFields to get information from the user at the keyboard and writes the information to a random access file.

enter.addActionListener( new ActionListener () { public void actionPerformed( ActionEvent event ){ addRecord(); } } ); setSize( 300, 150 ); setVisible( true ); } This program uses TextFields to get information from the user at the keyboard and writes the information to a random access file.

public void addRecord() { int acctNum = 0; Double d; acctNum = ( new Integer( acct.getText() ) ).intValue(); // output the values to the file try { if ( acctNum > 0 && acctNum <= 100 ) { String f=fName.getText(); String l=lName.getText(); double b=Double.parseDouble(bal.getText()); data = new Record(acctNum,f,l,b); output.seek( (long) ( acctNum-1 ) * data.size() ); data.write( output ); // clear the TextFields acct.setText( "" ); fName.setText( "" ); lName.setText( "" ); bal.setText( "" ); } else { acct.setText( "Enter valid account (1-100)" ); acct.selectAll(); } catch ( IOException e ) { System.err.println( "Error during write to file\n" + e.toString() ); System.exit( 1 ); } This program uses TextFields to get information from the user at the keyboard and writes the information to a random access file. seek method of RandomAccessFile : sets file-position pointer to a specific point in file

public void cleanup() { if ( !acct.getText().equals("") ) addRecord(); try { output.close(); } catch ( IOException e ) { System.err.println( "File not closed properly\n" + e.toString() ); System.exit( 1 ); } public static void main( String args[] ) { WriteRandFile accounts = new WriteRandFile(); } This program uses TextFields to get information from the user at the keyboard and writes the information to a random access file.

import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ReadRandFile extends JFrame { // Application window components JTextField acct, // where user enters account number fName, // where user enters first name lName, // where user enters last name bal; // where user enters balance JButton next, // send record to file done; // quit program JLabel acctLabel, // account label fNameLabel, // first name label lNameLabel, // last name label balLabel; // balance label RandomAccessFile input; // file for output Record data; boolean moreRecords=true; public ReadRandFile() { super( "Write to random access file" ); data = new Record(); try { input = new RandomAccessFile( "credit.dat", "r" ); } catch ( IOException e ) { System.err.println( e.toString() ); System.exit( 1 ); } This program opens a random-access file and displays only the records with data.

Container c = getContentPane(); c.setLayout( new GridLayout( 5, 2 ) ); acct = new JTextField( 20 ); acctLabel = new JLabel( "Account Number" ); fName = new JTextField( 20 ); fNameLabel = new JLabel( "First Name" ); lName = new JTextField( 20 ); lNameLabel = new JLabel( "Last Name" ); bal = new JTextField( 20 ); balLabel = new JLabel( "Balance" ); next = new JButton( "Next" ); done = new JButton( "Done" ); c.add( acctLabel ); // add label c.add( acct ); // add TextField c.add( fNameLabel ); // add label c.add( fName ); // add TextField c.add( lNameLabel ); // add label c.add( lName ); // add TextField c.add( balLabel ); // add label c.add( bal ); // add TextField c.add( next ); // add button c.add( done ); // add button This program opens a random-access file and displays only the records with data.

done.addActionListener( new ActionListener () { public void actionPerformed( ActionEvent event ){ cleanup(); // write data, close file, etc. setVisible(false)(); dispose(); // release system resources System.exit( 0 ); } } ); next.addActionListener( new ActionListener () { public void actionPerformed( ActionEvent event ){ readRecord(); } } ); setSize( 300, 150 ); setVisible( true ); } This program opens a random-access file and displays only the records with data.

public void readRecord() { try { do { // loop over empty records data.read( input ); } while ( input.getFilePointer() < input.length() && data.getAccount() == 0 ); } catch( IOException e ) { moreRecords = false; } // transfer full record data into textfields if ( data.getAccount() != 0 ) { acct.setText( String.valueOf( data.getAccount() ) ); String fN=data.getfName(); fName.setText( fN); String lN=data.getlName(); lName.setText( lN ); bal.setText( String.valueOf( data.getBalance() ) ); } This program opens a random-access file and displays only the records with data.

public void cleanup() { try { input.close(); } catch ( IOException e ) { System.err.println( e.toString() ); System.exit( 1 ); } } public static void main( String args[] ) { ReadRandFile accounts = new ReadRandFile(); } This program opens a random-access file and displays only the records with data.

Application: Transaction Processing Substantial transaction-processing system –Uses random-access file –Updates, adds and deletes accounts