File Input & Output1 Streams & File I/O. Introduction (1) The Java platform includes a number of packages that are concerned with the movement of data.

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.
Chapter 19 Binary I/O.
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.
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.
Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
File input and output. Interactive vs. file I/O All of the programs we have seen or written thus far have assumed interaction with a user, who types in.
King Saud University College of Computer and Information Sciences Department of Computer Science Dr. S. HAMMAMI Chapter 3 File Input/Output Chapter 3 File.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 11 File Input and Output.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
Simple Java I/O Part I General Principles. 2 Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Introduction to Input and Output. Layers (or Tiers) of an Application  Software in the real world normally takes the form of a number of independent.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Files and Streams CS 21a. 10/02/05 L18: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
Unit 201 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
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.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
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.
Files and Streams (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
CSC – Java Programming II Lecture 9 January 30, 2002.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java I/O Writing and Reading Objects to File Serialization.
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
Object Persistence and Object serialization CSNB534 Asma Shakil.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Selection sort and Merge sort File input/output. HW 2 – Section 02 Avg = 96/100.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
 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.
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.
Chapter 11 Exceptions and Input/Output Operations.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
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.
Chapter - 11 Introduction to File and Streams This chapter includes -  Defining a File  Testing and Checking File Objects  Accessing File Objects.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
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.
Generics and file handling Presented by: Abha Kumari Neha Pradip Vhanmane Raj Visa Rashmi Kewlani Suvrat Dixit.
Chapter 10: I/O Streams Input Streams Output Streams
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Object Writing in files
I/O Basics.
Chapter 17 Binary I/O 1.
Accessing Files in Java
Programming in Java Files and I/O Streams
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Chapter 12 File Input and Output
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Presentation transcript:

File Input & Output1 Streams & File I/O

Introduction (1) The Java platform includes a number of packages that are concerned with the movement of data into and out of programs. These packages differ in the kinds of abstractions they provide for dealing with I/O (input/output). The java.io package defines I/O in terms of streams. Streams are ordered sequences of data that have a source (input streams) or destination (output streams). The I/O classes isolate programmers from the specific details of the underlying operating system, while enabling access to system resources through files and other means. Most stream types (such as those dealing with files) support the methods of some basic interfaces and abstract classes, with few (if any) additions. The best way to understand the I/O package is to start with the basic interfaces and abstract classes. File Input & Output2

Introduction (2) An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays. Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways. No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a sequence of data. File Input & Output3

Introduction (3) A program uses an output stream to write data to a destination, one item at time: File Input & Output4 A program uses an input stream to read data from a source, one item at time:

File Input & Output5 Agenda This Section will cover the following : Introduce a FileDialog object which allows the user to specify a file Write bytes to a file and read them back from the file using FileOutputStream and FileInputStream Write values of primitive data types to/from a file using DataOutputStream and DataInputStream Write Objects to/from a file using ObjectOutputStream and ObjectInputStream Write exception-handling routines using the try-catch block

File Input & Output6 File Objects (1) Java supplies two key objects for reading and writing to and from files : A File Object A FileDialog Object Suppose we want to read the contents of the file “Sample.dat”. 1 st we must create a File object (from the java.io package) 2 nd we must associate the File object with the file itself. This is achieved as follows : File afile = new File(“Sample.dat”); Note : This assumes the file Sample.dat is stored in the current directory.

File Input & Output7 File Objects (2) The argument to the constructor (sample.dat), specifies the file to access. To open a file that is stored in a directory other than the current directory you must also specify a path name. File afile = new File(“C:\\MyFiles”,”abc.txt”); As a rule, you should also check to see if a File object has correctly been associated to an exising file, by calling its exists method. if( afile.exists() ) { … } If a vaild association is established, we say the file is opened, and we can now proceed with I/O. N.B.

File Input & Output8 Some commonly used File methods.

File Input & Output9 FileDialog Objects (1) We can let the user select a file or a directory, via a GUI, using a FileDialog object. The object has 2 modes : LOAD : to read data from the specified file SAVE : to write data to the specified/selected file The Following code is used to display the Dialog Box below (on the next slide) for Opening a File FileDialog aFileDialog; Frame aFrame = new Frame("My Frame"); //required for most apps aFileDialog = new FileDialog(aFrame,"Open File",FileDialog.LOAD); aFileDialog.setVisible (true); String aFileName = aFileDialog.getFile( ); // copied from slide 11 File aFile = new File(aFileName); // copied from slide 11

File Input & Output10 FileDialog Objects (2) The “Open File” Dialog Box: The user can then select a file from the list of files. Open Option

File Input & Output11 FileDialog Objects (3) To retrieve the name of the file the user has selected, we use the getFile() method. String aFileName = aFileDialog.getFile( ); If the user has selected ‘Cancel’ the method returns a null string. Once we have a filename we can create a new File object. File aFile = new File(aFileName); To select a file for saving data, we create and open the FileDialog in the SAVE mode: aFileDialog = new FileDialog(aFrame, "Save As",FileDialog.SAVE); aFileDialog.setVisible (true);

File Input & Output12 Low-Level File I/O (1) – Byte Streams Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream. Once a file is opened (associated properly with a File object), the actual file access can commence. In order to read/write from/to a file, we must create one of the Java stream objects and attach it to a file. There are many byte stream classes available to us. To demonstrate how byte streams work, we'll focus on the file I/O byte streams, FileInputStream and FileOutputStream. Other kinds of byte streams are used in much the same way; they differ mainly in the way they are constructed. To actually read the data from a file, we attach one of the Java Input Stream objects to the file (ditto for writing)

File Input & Output13 Low-Level File I/O (2) As already mentioned, the two objects that provide low-level (byte) file access are: FileOutputStream FileInputStream With these objects, we can only input/output a sequences of bytes i.e. values of data type byte (we will look at writing other data types and even objects later on) To write information to a file we can do the following: First, create our File object  File aFile = new File(“Sample.dat”); // or use the FileDialog Object Second, associate a new FileOutputStream object to a File  FileOutputStream outByteStream = new FileOutputStream(aFile); Now we are ready for output

File Input & Output14 Low-Level File I/O (3) Consider the following byte array: byte byteArray[ ] = {10,20,30,40,50,60}; We can write out the whole array at once as follows: outByteStream.write(byteArray); Or elements Individually: outByteStream.write(byteArray[0]); Once the values have been written to the file, we must close the stream: outByteStream.close(); If a stream is not closed, what are the implications?

File Input & Output15 Low-Level File I/O (4) To read data into a program, we reverse the steps in the output routine: First, create our File object (if one doesn’t already exist)  File aFile = new File(“Sample.dat “ ); // or use the FileDialog Object Second, associate a new FileInputStream object to a File  FileInputStream inByteStream = new FileInputStream(aFile); Before we can read in the data, we must first declare and create a byteArray: int filesize = (int) aFile.length() byte byteArray[ ] = new byte[filesize]; We use the length() method of the File class to determine the size of the file (the number of bytes in the file). We can then read in the data as follows: inByteStream.read(byteArray);

File Input & Output16 Mid-Level File I/O (1) – Data Streams Data streams support binary I/O of primitive data type values (boolean, char, byte, short, int, long, float, and double) as well as String values. All data streams implement either the DataInput interface or the DataOutput interface. This section focuses on the most widely-used implementations of these interfaces, DataInputStream and DataOutputStream. The Following code creates a DataOutputStream object: File aFile = new File(“Sample1.dat”); // or use the FileDialog Object FileOutputStream outByteStream = new FileOutputStream(aFile); DataOutputStream outDataStream = new DataOutputStream(outByteStream); N.B. - Since a DataOutputStream can only be created as a wrapper for an existing byte stream object, the argument to the DataOutputStream constructor is a FileOutputStream object. A DataOutputStream object does not get connected to a file directly – it’s connected via a FileOutputStream object (ditto for reading…)

File Input & Output17 Mid-Level File I/O (2) The DataOutputStream object has a number of methods for writing the primitive data types: outDataStream.writeInt( ); outDataStream.writeDouble( ); outDataStream.writeChar(‘A’); outDataStream.writeBoolean(true); Don’t forget to close the stream outDataStream.close();

File Input & Output18 Mid-Level File I/O (3) To read the data back from the file, we reverse the operation: File aFile = new File(“Sample1.dat”); // or use the FileDialog Object FileInputStream inByteStream = new FileInputStream(aFile); DataInputStream inDataStream = new DataInputStream(inByteStream); ….. int x= inDataStream.readInt(); double d = inDataStream.readDouble(); char Y inDataStream.readChar(); boolean b = inDataStream.readBoolean(); …… inDataStream.close(); Note : the order of write & read operations must match – Why?

File Input & Output19 Mid-Level File I/O : Exceptions (1) Exceptions are usually handled by catching a thrown exception and providing exception-handling code to process the thrown exception. There are two approaches to handling thrown exceptions: Add a throw clause to the method header or Use a try/catch block in your code For simple programs, the first approach may be acceptable, but in general you should use the second approach, in which you write a response code to handle a thrown exception.

File Input & Output20 Mid-Level File I/O : Exceptions (2) Take the following example : class FindSum { private int sum; private boolean success; public int GetSum( ) { return sum; } public boolean isSuccess( ) { return success; }

File Input & Output21 Mid-Level File I/O : Exceptions (3) void computeSum(String Filename) { success = true; try { File aFile = new File(FileName); FileInputStream inByteStream = new FileInputStream(aFile); DataInputStream inDataStream = new FileDataStream(inByteStream); int i = inDataStream.readInt(); int j = inDataStream.readInt(); int k = inDataStream.readInt(); sum = i + j + k; inDataStream.close(); } // end of try block catch( IOException e ) { success = false; } } // end of method computeSum } // end of class FindSum

File Input & Output22 Mid-Level File I/O : Exceptions (4) // In a Program FindSum findTheSum = new FindSum( ); findTheSum.computeSum(“samplint.dat”); if( findTheSum.isSuccess( ) ) { int total = findTheSum.getSum( ); // Output total } else { // Output some File I/O Error Message }

File Input & Output23 Mid-Level File I/O : Exceptions (5) If an exception is thrown (i.e. an error occurs) during the execution of the try block, then the catch block is executed and the variable success is set to false. On a call to the method isSuccess( ), a false value is returned and an error message is displayed. We can modify the catch block to output an error message itself, as well as setting the success variable: try {…} catch (IOException e) { success = false; JOptionPane.showMessageDialog(null,e.toString(),”Error Message”, JOptionPane.ERROR_MESSAGE); }

File Input & Output24 Mid-Level File I/O : Exceptions (6) There are a number of useful exception classes available, when working with files : You can throw a FileNotFoundException if you’re trying to open a file that does not exist. You can throw a EOFException if you’re trying to read beyond the end of a file If you want to include statements that will catch any type of exception you can use the following: catch( Exception e) { … }

File Input & Output25 High-Level File I/O (1) - Object Streams Just as 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 that do implement the marker interface Serializable. The object stream classes are ObjectInputStream and ObjectOutputStream. These classes implement ObjectInput and ObjectOutput, which are subinterfaces of DataInput and DataOutput. That means that all the primitive data I/O methods covered in Data Streams are also implemented in object streams. So an object stream can contain a mixture of primitive and object values. We will take a Person Object as an example to illustrate. We assume a Person Object consists of : A Name (String) An Age (int) A Gender (char), (‘M’ or ‘F’)

File Input & Output26 High-Level File I/O (2) We must first ensure that the definition of a class Person allows for Object I/O. This is achieved by making the class serializable!! For example: import java.io.*; // we need this for class Serializable class Person implements Serializable { // All the declarations / methods } If the class is a subclass then the class definition would be something like this: class Student extends Person implements Serializable { …

File Input & Output27 High-Level File I/O (3) To write a Person object to a file, we first create an ObjectOutputStream object: File aFile = new File(“objects.dat”); FileOutputStream outByteStream = new FileOutputStream(aFile); ObjectOutputStream ooStream = new ObjectOutputStream(outByteStream); To save a Person object, we write : Person aPerson = new Person(“Joe Bloggs”, 25, ‘M’); ooStream.writeObject( aPerson ); Note : You can save different types of objects to the same file using the writeObject( ) method.

File Input & Output28 High-Level File I/O (4) To read a Person object from a file, we first create an ObjectInputStream object: File aFile = new File(“objects.dat”); FileInputStream inByteStream = new FileInputStream(aFile); ObjectInputStream oiStream = new ObjectInputStream(inByteStream); To read a Person object from the file, we write : Person aPerson = (Person) oiStream.readObject( ); Note : If you save different types of objects to the same file using the writeObject( ) method you must ensure that the objects are read back in the correct order, using readObject( )

File Input & Output29 High-Level File I/O (5) Consider the following array of Person objects where n represents some integer value. Person group[ ] = new Person[ n ]; Assuming that all N Person objects are in the Array, we can store them to a file a follows: //Save the size of the array first ooStream.writeInt( group.length ); //Save Person Objects next for(int i = 0; i< group.length; i++) ooStream.writeObject( group[ i ] ); We store the size of the array first, so we know how many Person objects to read back.

File Input & Output30 High-Level File I/O (6) int n = oiStream.readInt(); for(int i = 0; i< n ; i++) group[ i ] = (Person) oiStream.readObject( ); However, since an array itself is an object, we can actually store the whole array at once : ooStream.writeObject( Ggoup ); And read the whole array back at once : group = ( Person[ ] ) oiStream.readObject( ); Note the type casting of an array of Person objects

File Input & Output31 Case Study : Student Example (1) import javax.swing.JOptionPane; import java.io.*; class Student implements Serializable { private long ID; private String Name; public Student() { ID = 0; Name = null; } public void setData(long id, String name) { ID = id;Name = name;} public String toString() { return "Student ID : " + ID + "\nStudent Name : " + Name; } } // end of class Student

File Input & Output32 Case Study : Student Example (2) import java.awt.*; import javax.swing.JOptionPane; import java.io.*; public class FileTester { public static void main(String[] args) { try { Student Stu = new Student(); Student Stu1 = new Student(); Student StuArray[] = new Student[10]; Stu.setData (123456, "Joe Bloggs"); Stu1.setData(123443,"Mrs Joe Bloggs"); StuArray[0] = Stu; StuArray[1] = Stu1;

File Input & Output33 Case Study : Student Example (3) FileDialog FileBox; Frame MW = new Frame("My Frame"); FileBox = new FileDialog(MW,"Open File",FileDialog.LOAD); FileBox.setVisible (true); String S = FileBox.getFile (); JOptionPane.showMessageDialog (null,"The File You Selected was: " + S,"File Opened",JOptionPane.QUESTION_MESSAGE ); File infile = new File(S); FileInputStream inbytestream = new FileInputStream(infile);

File Input & Output34 Case Study : Student Example (4) if (infile.length () != 0) { ObjectInputStream IIStream = new ObjectInputStream(inbytestream); StuArray = (Student[]) IIStream.readObject (); int i = 0; while(i < 2) { JOptionPane.showMessageDialog (null,StuArray[i],"Student Details",JOptionPane.INFORMATION_MESSAGE); i++; } inbytestream.close (); IIStream.close (); }

File Input & Output35 Case Study : Student Example (5) File outfile = new File(S); FileOutputStream outbytestream = new FileOutputStream(outfile); ObjectOutputStream OOStream = new ObjectOutputStream(outbytestream); OOStream.writeObject (StuArray); MW.setVisible (true); FileBox = new FileDialog(MW,"Save File",FileDialog.SAVE ); FileBox.setVisible (true); outbytestream.close (); OOStream.close (); }

File Input & Output36 Case Study : Student Example (6) catch(Exception e) { JOptionPane.showMessageDialog (null,"Error :" + e.toString(),"File I/O Error",JOptionPane.ERROR_MESSAGE ); } System.exit (0); }