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.

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.
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.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
File Handling and Serialization CSIS 3701: Advanced Object Oriented Programming.
Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
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.
©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 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.
Lecture 7 File I/O (and a little bit about exceptions)‏
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
Unit 201 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
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.
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.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
15-Jul-15 Simple Java I/O Part I General Principles.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
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.
Handling errors Exception handling and throwing Simple file processing.
CIS 270—Application Development II Chapter 14—Files and Streams.
Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Text Input and Output on the Console.
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,
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 I/O Writing and Reading Objects to File Serialization.
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
OOP with Java, David J. Barnes Input-Output1 A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
© Amir Kirsh Files and Streams in Java Written by Amir Kirsh.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
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.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
Files and Serialization. Files Used to transfer data to and from secondary storage.
CSI 3125, Preliminaries, page 1 Files. CSI 3125, Preliminaries, page 2 Reading and Writing Files Java provides a number of classes and methods that allow.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
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.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 (3 weeks)
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.
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
CSG2H3 Object Oriented Programming
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 21_1 GEORGE KOUTSOGIANNAKIS
I/O Basics.
Accessing Files in Java
CS 116 Object Oriented Programming II
Chapter 12 File Input and Output
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Java Basics Introduction to Streams.
David Davenport Spring 2005
Presentation transcript:

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 and output devices; explain the concept of an I/O stream; describe the basic file-handling techniques used in the Java language; distinguish between text, binary and object encoding of data; distinguish between serial access files and random access files; create and access files in Java using all the above encoding and access methods.

Input and output Output: sending information back to the outside world. Input: receiving information from the outside world standard input stream standard output stream file input stream file output stream

Encoding Java supports three different ways of encoding data: “Java” “Charatan Kans” “34.99” TEXT Can be read by text editors as well as programs. BINARY Can only programs. OBJECT Can only read by another Java program.

Types of File Access Serial access Each item of data is read (or written) in turn. Random access We go directly to the record we want.

The Car Class public class Car { private String registration; private String make; private double price; public Car (String registrationIn, String makeIn, double priceIn) { registration = registrationIn; make = makeIn; price = priceIn; } public String getRegistration() { return registration; } public String getMake() { return make; } public double getPrice() { return price; }

Writing to text files “XV1 2AS” “Ford” “2499” Cars.txt FileWriter PrintWriter

private static void writeList (List<Car> carListIn) { try { FileWriter carFile = new FileWriter ("Cars.txt"); PrintWriter carWriter = new PrintWriter (carFile); for (Car item : carListIn) { carWriter.println ( item.getRegistration() ); carWriter.println ( item.getMake() ); carWriter.println ( item.getPrice() ); } carWriter.close(); } catch (IOException e) { System.out.println("There was a problem writing the file"); } }

Effects of writing to a text file

Reading from text files “XV1 2AS” “Ford” “2499” Cars.txt FileReader BufferedReader

private static void readList (List<Car> carListIn) { String tempReg; String tempMake; String tempStringPrice; double tempDoublePrice; try { FileReader carFile = new FileReader ("Cars.txt"); BufferedReader carStream = new BufferedReader (carFile); tempReg = carStream.readLine(); while (tempReg != null) { // code to read data goes here } carStream.close(); } // code to catch exceptions goes here }

tempMake = carStream.readLine(); tempStringPrice = carStream.readLine(); tempDoublePrice = Double.parseDouble(tempStringPrice); carListIn.add (new Car(tempReg,tempMake,tempDoublePrice)); tempReg = carStream.readLine(); Code to read data

catch(FileNotFoundException e) { System.out.println("\nNo file was read"); } catch(IOException e) { System.out.println ("\nThere was a problem reading the file"); } Code to catch exceptions

Writing to binary files Cars.bin FileOutputStream DataOutputStream

private static void writeList (List<Car> carListIn) { Car tempCar; try { FileOutputStream carFile = new FileOutputStream("Cars.bin"); DataOutputStream carWriter = new DataOutputStream(carFile); for (Car item : carListIn) { carWriter.writeUTF(item.getRegistration()); carWriter.writeUTF(item.getMake()); carWriter.writeDouble(item.getPrice()); } carWriter.close(); } catch(IOException e) { System.out.println("There was a problem writing the file"); } }

The effects of writing to a binary file

Reading from binary files Cars.bin FileInputStream DataInputStream

Reading from a binary file private static void readList (List<Car> carListIn) { String tempReg; String tempMake; double tempPrice; boolean endOfFile = false; try { FileInputStream carFile = new FileInputStream ("Cars.bin"); DataInputStream carStream = new DataInputStream (carFile); while(endOfFile == false) { // code to read data here } carStream.close(); } // code to catch FileNotFoundException and IOException here }

Code to read data try { tempReg = carStream.readUTF(); tempMake = carStream.readUTF(); tempPrice = carStream.readDouble(); carListIn.add(new Car(tempReg, tempMake, tempPrice)); } catch(EOFException e) { endOfFile = true; }

Object serialization Enables us to read and write whole objects from and to files; The process of converting an object into a stream of data suitable for storage on a disk is called serialization; Any class whose objects are to be read and written using the above methods must implement the interface Serializable ; import java.io.*; public class Car implements Serializable { // rest of code here }

Writing to object files Cars.obf FileOutputStream ObjectOutputStream

Writing objects to a file private static void writeList(List<Car> carListIn) { try { FileOutputStream carFile = new FileOutputStream("Cars.obf"); ObjectOutputStream carStream = new ObjectOutputStream(carFile); for (Car item : carListIn) { carStream.writeObject(item); } carStream.close(); } catch(IOException e) { System.out.println("There was a problem writing the file"); } }

Reading from object files Cars.obf FileInputStream ObjectInputStream

Reading objects from a file private static void readList(List<Car> carListIn) { boolean endOfFile = false; Car tempCar; try { FileInputStream carFile = new FileInputStream("Cars.obf"); ObjectInputStream carStream = new ObjectInputStream(carFile); tempCar = (Car) carStream.readObject(); while(endOfFile != true) { // code to read data here } carStream.close(); } // code to catch exceptions here }

try { carListIn.add(tempCar); tempCar = (Car) carStream.readObject(); } catch(EOFException e) { endOfFile = true; } Code to read data

catch(FileNotFoundException e) { System.out.println("\nNo file was read"); } catch(ClassNotFoundException e) // thrown by readObject { System.out.println ("\nTrying to read an object of an unknown class"); } catch(StreamCorruptedException e) // thrown by the constructor { System.out.println("\nUnreadable file format"); } catch(IOException e) { System.out.println("There was a problem reading the file"); }