MSIS 670: Object-Oriented Software Engineering

Slides:



Advertisements
Similar presentations
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
Advertisements

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.
Chapter 11 File-System Interface
Files & Streams. Files Introduction Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
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.
© The McGraw-Hill Companies, 2006 Working with files Chapter 20.
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.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
13.1 Understanding Files The File class Objects can read and write to the file system Use the File class to hold information about files and directories.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Files and Streams (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Topics Introduction Hardware and Software How Computers Store Data
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.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
File Systems Long-term Information Storage Store large amounts of information Information must survive the termination of the process using it Multiple.
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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes.
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Files and Streams Chapter What You Will Learn Create files Read files Write files Update files.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
 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.
Object Serialization. Sequential-access Text Files Sequential-access files store records In order by the record-key field Java imposes no structure on.
1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
 Pearson Education, Inc. All rights reserved Files and Streams.
Java Programming, Second Edition Chapter Sixteen File Input and Output.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
 2005 Pearson Education, Inc. All rights reserved Files and Streams.
Files and Streams Lec Introduction  Files are used for long-term retention of large amounts of data, even after the program that created the.
CSG2H3 Object Oriented Programming
Chapter 3 Data Representation
File Processing.
11 C File Processing.
17 File Processing.
Ch14 Files and Streams OBJECTIVES
Topics Introduction Hardware and Software How Computers Store Data
TMF1414 Introduction to Programming
Files.
I/O Basics.
Chapter 13: File Input and Output
17 File Processing.
Chapter 11 – File Processing
Chapter 17 – Files and Streams
Reading and Writing Files
17 File Processing.
14 Files and Streams.
files Dr. Bhargavi Goswami Department of Computer Science
Topics Introduction Hardware and Software How Computers Store Data
Files & Streams.
Text and Binary File Processing
Fundamental of Programming (C)
14 Files and Streams.
Chapter 15 Files, Streams and Object Serialization
PROGRAMMING CONCEPTS CHAPTER 8
Presentation transcript:

MSIS 670: Object-Oriented Software Engineering Week 7 Files and Streams In this chapter you will learn: To create, read, write and update files. To use class File to retrieve information about files and directories. Sequential-access and random-access file processing. To use a JFileChooser dialog. To use class RandomAccessFile. 11/27/2018 7.1

Ch. 14: Files and Streams 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 Storage of data in variables and arrays is temporary Files used for long-term retention of large amounts of data, even after the programs that created the data terminate Persistent data – exists beyond the duration of program execution Files stored on secondary storage devices Stream – ordered data that is read from or written to a file 11/27/2018

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 11/27/2018

Data Hierarchy Data hierarchy Records 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 Fig. 14.1 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 Fields – a group of characters or bytes that conveys meaning Record – a group of related fields File – a group of related records Data items processed by computers form a data hierarchy that becomes larger and more complex from bits to files Record key – identifies a record as belonging to a particular person or entity – used for easy retrieval of specific records Sequential file – file in which records are stored in order by the record-key field Database – a group of related files Database Management System – a collection of programs designed to create and manage databases 11/27/2018

14.4 Class File Class File Provides useful information about a file or directory Does not open files or process files Fig. 14.3 (p.692) lists some useful File methods 11/27/2018

Testing a class -File- (p. 695, Fig. 14.5) With text files, data type information lost Object serialization – mechanism to read or write an entire object from a file Serialized object – object represented as sequence of bytes, includes object’s data and type information about object Deserialization – recreate object in memory from data in file Serialization and deserialization performed with classes ObjectInputStream and ObjectOutputStream, methods readObject and writeObject 11/27/2018

Creating a Sequential-Access File Java files Java imposes no structure on a file Notions like “record” do not exist in Java files (record vs. stream of bytes). Programmer structures file according to application File streams Byte-based streams – stores data in binary format Binary files – created from byte-based streams, read by a program that converts data to human-readable format Character-based streams – stores data as a sequence of characters Text files – created from character-based streams, can be read by text editors Java opens file by creating an object and associating a stream with it Standard streams – each stream can be redirected System.in – standard input stream object, can be redirected with method setIn System.out – standard output stream object, can be redirected with method setOut System.err – standard error stream object, can be redirected with method setErr 11/27/2018

CreateSequentialFIle (Example 1) A JFrame class, importing two classes BankUI AccountRecord CreateSequentialFile.java openFile uses JFileChooser (c.f. 14.9 – p.729) Creates ObjectOutputStream object (output) (LL. 127-128) java.io classes FileInputStream and FileOutputStream – byte-based I/O FileReader and FileWriter – character-based I/O ObjectInputStream and ObjectOutputStream – used for input and output of objects or variables of primitive data types File – useful for obtaining information about files and directories Classes Scanner and Formatter Scanner – can be used to easily read data from a file Formatter – can be used to easily write data to a file Formatter class can be used to open a text file for writing Pass name of file to constructor If file does not exist, will be created If file already exists, contents are truncated (discarded) Use method format to write formatted text to file Use method close to close the Formatter object (if method not called, OS normally closes file when program exits) 11/27/2018

CreateSequentialFIle (cont.) addRecord method Creates an object record (L.181) writeObject method write the record to output (ObjectOutputStream) (L.188) Scanner object can be used to read data sequentially from a text file Pass File object representing file to be read to Scanner constructor FileNotFoundException occurs if file cannot be found Data read from file using same methods as for keyboard input – nextInt, nextDouble, next, etc. IllegalStateException occurs if attempt is made to read from closed Scanner object Fig.14.8 | End-of-file key combinations for various popular operating systems BankUI 11/27/2018

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 Data in many sequential files cannot be modified without risk of destroying other data in file Old data cannot be overwritten if new data is not same size Records in sequential-access files are not usually updated in place. Instead, entire file is usually rewritten. 11/27/2018

Random-Access File “Instant-access” applications Random-access files 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 Fig. 17.10 shows random-access file Fig. 16.10 Java’s view of a random-access file. 11/27/2018

Creating a 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 Sequential-access files inappropriate for instant-access applications Instant-access applications are applications in which desired information must be located immediately Instant access possible with random-access files (also called direct-access files) and databases Data can be inserted in random-access file without destroying other data Different techniques for creating random-access files Simplest: Require that all records in file be same fixed length Easy to calculate (as a function of record size and record key) exact location of any record relative to beginning of file 11/27/2018

Writing Data Randomly to a Random-Access File RandomAccessFile method seek Determines location in file where record is stored Sets file-position pointer to a specific point in file Open file with “r” file-open mode for reading Ignore empty records (usually those with account number of zero) when reading from file Records stored by account number in random-access files have added bonus of being sorted, as each record’s data can only be placed in specific portion of file Sorting with direct-access techniques is blazingly fast—speed achieved by making file large enough to hold every possible record Space/time trade-off 11/27/2018

Example 2: A Transaction-Processing Program Source code would be on the Web Substantial transaction-processing system Uses random-access file Updates, adds and deletes accounts Different from the Textbook (use of UI) 11/27/2018