Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.

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

Chapter 19 Binary I/O.
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.
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.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming.
Chapter 10 File I/O Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
CS102--Object Oriented Programming Lecture 13: File I/O Copyright © 2008 Xiaoyan Li.
Lecture 7 File I/O (and a little bit about exceptions)‏
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
© The McGraw-Hill Companies, 2006 Working with files Chapter 20.
Chapter 10 File I/O Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
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 91 Streams and File I/O CS-180 Recitation-03/07/2008.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 19 Binary I/O.
Introduction To Scientific Programming Chapter 9 – Stream & File I/O.
1 Chapter 15 – Files I/O basics File I/O Classes File I/O Basic Operations Text File Output PrintWriter import Statement with a * Text File Input Scanner,
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.
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.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 15 – Files I/O basics File I/O Classes File I/O Basic Operations Text File Output PrintWriter import Statement with a * Text File Input Scanner,
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.
Lecturer: Dr. AJ Bieszczad Chapter 9 COMP 150: Introduction to Object-Oriented Programming 9-1 l Overview of Streams and File I/O l Text File I/O l Binary.
Two Ways to Store Data in a File  Text format  Binary format.
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.
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.
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.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
 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.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Binary I/O.
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 10 Introduction to File I/O Section 10.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 10 File I/O Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 (3 weeks)
Up to slide 46 for 111. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Text File I/O.
Introduction to programming in java
Chapter 17 Binary I/O.
Object Writing in files
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Chapter 17 Binary I/O 1.
Chapter 10 File I/O Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 10 File I/O Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Streams and File I/O Chapter 9 Chapter 9.
Chapter 17 Binary I/O Dr. Clincy - Lecture.
CSS 161: Fundamentals of Computing
Comp 249 Programming Methodology
Introduction to Classes and Methods
Chapter 10 File I/O Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
Presentation transcript:

Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files

Unit 212 Binary Files Files that are designed to be read by programs and that consist of a sequence of binary digits are called binary files Binary files store data in the same format used by computer memory to store the values of variables –No conversion needs to be performed when a value is stored or retrieved from a binary file Java binary files, unlike other binary language files, are portable –A binary file created by a Java program can be moved from one computer to another –These files can then be read by a Java program, but only by a Java program

Unit 213 Writing to a Binary File The class ObjectOutputStream is a stream class that can be used to write to a binary file –An object of this class has methods to write strings, values of primitive types, and objects to a binary file An ObjectOutputStream object is created and connected to a binary file as follows: ObjectOutputStream outputStreamName = new ObjectOutputStream(new FileOutputStream(FileName));

Unit 214 Writing to a Binary File (continued) After opening the file, ObjectOutputStream methods can be used to write to the file –Methods used to output primitive values include writeInt, writeDouble, writeChar, and writeBoolean UTF is an encoding scheme used to encode Unicode characters that favors the ASCII character set –The method writeUTF can be used to output values of type String The stream should always be closed after writing

Unit 215 Example 1 1.import java.io.*; 2.public class BinaryFiles 3.{ 4.public static void main(String args[]) 5.{ 6. try{ 7.ObjectOutputStream outputStreamName = new 8. ObjectOutputStream(new FileOutputStream("output.txt")); 9. int i = 45; double j = 3.4; char k = 'a'; 10. outputStreamName.writeInt(i); 11. outputStreamName.writeDouble(j); 12. outputStreamName.writeChar(k); 13. outputStreamName.close(); 14. }catch(IOException e){} 15.}} File data if opened through any editor ’ w a

Unit 216 Reading from a Binary File The class ObjectInputStream is a stream class that can be used to read from a binary file –An object of this class has methods to read strings, values of primitive types, and objects from a binary file An ObjectInputStream object is created and connected to a binary file as follows: ObjectInputStream inStreamName = new ObjectInputStream(new FileInputStream(FileName));

Unit 217 Reading From a Binary File (continued) After opening the file, ObjectInputStream methods can be used to read to the file –Methods used to input primitive values include readInt, readDouble, readChar, and readBoolean –The method readUTF is used to input values of type String If the file contains multiple types, each item type must be read in exactly the same order it was written to the file The stream should be closed after reading

Unit 218 Example 2 1.public class BinaryFiles 2.{ 3.public static void main(String args[]) 4.{ 5.try{ 6.ObjectInputStream inStreamName = new 7. ObjectInputStream(new 8. FileInputStream("output.txt")); 9. int i = inStreamName.readInt(); 10. double j = inStreamName.readDouble(); 11. char k = inStreamName.readChar(); 12. System.out.println(i+" "+ j+" "+k); 13. }catch(IOException e){} 14.}} Output: a

Unit 219 Checking for the End of a Binary File All of the ObjectInputStream methods that read from a binary file throw an EOFException when trying to read beyond the end of a file –This can be used to end a loop that reads all the data in a file Note that different file-reading methods check for the end of a file in different ways –Testing for the end of a file in the wrong way can cause a program to go into an infinite loop or terminate abnormally

Unit 2110 Objects IO to Binary File Objects can also be input and output from a binary file –Use the writeObject method of the class ObjectOutputStream to write an object to a binary file –Use the readObject method of the class ObjectInputStream to read an object from a binary file –In order to use the value returned by readObject as an object of a class, it must be type cast first: SomeClass someObject = (SomeClass)objectInputStream.readObject();

Unit 2111 Object IO to Binary Files In addition, the class of the object being read or written must implement the Serializable interface In order to make a class serializable, simply add implements Serializable to the heading of the class definition public class SomeClass implements Serializable When a serializable class has instance variables of a class type, then all those classes must be serializable also –A class is not serializable unless the classes for all instance variables are also serializable for all levels of instance variables within classes

Unit 2112 Example A simple Student Class 1.class Student implements Serializable 2.{ 3.private String Name; 4.private int Age; 5.private String ID; 6.public Student(String Name, int Age, String ID) 7.{ 8.this.Name = Name; 9.this.Age = Age; 10.this.ID = ID; 11.} 12.public String toString() 13.{ 14.return Name + ":" + ID+":"+Age; 15.} 16.}

Unit 2113 Write Object to Binary File 1.public class BinaryFiles 2.{ 3.public static void main(String args[]) 4.{ 5.try{ 6.ObjectOutputStream outputStreamName = new 7. ObjectOutputStream(new FileOutputStream("output.txt")); 8. Student s = new Student("Ahmed",21,"232323"); 9. outputStreamName.writeObject(s); 10. outputStreamName.close(); 11. }catch(IOException e){} 12.}}

Unit 2114 Reading Objects 1.public class BinaryFiles 2.{ 3.public static void main(String args[]) 4.{ 5.try{ 6.ObjectInputStream inStreamName = new 7. ObjectInputStream(new 8. FileInputStream("output.txt")); 9. Student s = (Student) inStreamName.readObject(); 10. System.out.println(s); 11. }catch(IOException e){} 12. catch(ClassNotFoundException e){} 13.}} Output: Ahmed:232323:12

Unit 2115 Introduction to Scanner Class Java Introduced a new class called Scanner in jdk 1.5 This class can be used to read Data from the keyboard or from a file The following code will read an integer from the user Scanner sc = new Scanner(System.in); int i = sc.nextInt(); The following code will read and integer from a file Scanner sc = new Scanner(new FileInputStream(FileName)); Int I = sc.nextInt();

Unit 2116 Scanner Class (contd) Some Useful functions in the Scanner class –byte nextByte() reads the next BytenextByte –double nextDouble() reads the next Double –int nextInt() reads the next Integer –String nextLine() reads a complete Line Checkout for complete documentationhttp://java.sun.com/j2se/1.5.0/docs/api/