UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.

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

Java I/O.
1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
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.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Standard input, output and error. Lecture Under Construction.
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.
Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming.
©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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
Algorithm Programming I/O via Java Streams Bar-Ilan University תשס"ח by Moshe Fresko.
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.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Java I/O Input: information brought to program from an external source
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes.
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.
I / O in java. java.io BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
L5: Input & Output COMP206, Geoff Holmes and Bernhard Pfahringer.
Java Input/Output. Reading standard input Surprisingly complicated (GUI focus) Old-fashioned way: BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
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.
1 OOP Lecture 17 I/O and Graphics Signe Ellegård Borch Carsten Schuermann IT University Copenhagen.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
Streams & Files. Java I/O Classes Saving data to / Loading data from files Two Choices: Binary-Formatted or Text-Formatted Data – int x = 1234; – Binary.
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
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.
Company Input/Output Stream –. Input/Output Stream Data Program Input Stream Output Stream.
DEPARTMENT OF COMPUTER SCIENCE N.HARIKA. Contents Overview of I/O Streams Character Streams Byte Streams Using the Streams 2.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Java IO Exploring the java.io package and living to talk about it.
Chapter 10: I/O Streams Input Streams Output Streams
Fundamental of Java Programming
Sequential files creation & writing
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Files and Streams The material in this chapter is not tested on the AP CS exams.
CSG2H3 Object Oriented Programming
IO in java.
Exception Handling, Reading and Writing in Files, Serialization,
Ch14 Files and Streams OBJECTIVES
Chapter 8: Input and Output
Chapter 17 Binary I/O.
Chapter 17 Binary I/O 1.
Java Methods ("Chapter %d", 14); Streams and Files A & AB
Programming in Java Files and I/O Streams
Објектно орјентисано програмирање
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Stream Oriented I/O Computer Programming II Dr. Tim Margush
Files and Streams in Java
14 Files and Streams.
CS 240 – Advanced Programming Concepts
David Davenport Spring 2005
Presentation transcript:

UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an Object Wed. 10/4/00

Homework #3 1 Fri, 9/8 Fri, 9/15 Part 1 Mon, 9/18 Part 2 Mon, 9/18 Part 2 2Fri, 9/15 Fri, 9/22 Part 1 & Part 2 3Fri, 9/22 Fri, 9/29Part 1 & Part 2 HW# Assigned Due Content Homework is due at the start of lecture on the due date. Homework #4 will be assigned on Fri., 10/6 (skipping one week due to test)

Exam 1: Closed Book Topics not covered: Jini, Exception Handling, Files/Streams Format: Multiple choice

Java File I/O Focusing on Serialization and Sequential Files [Sources: Deitel, Java 2 Certification, Java 2: The Complete Reference]

Java I/O Philosophy ä Console I/O is limited: ä Text-based console programs are not part of the main Java design focus ä Applets have limited access to resources ä Device-independent abstraction

Java Streams ä Motivated by C++ stream concept: ä remember cin >> and cout > and cout << ? ä Stream: ä Sequence of bytes or characters that travel from a source to a destination over a communications path ä Relieves programmer of burden of communication path details ä Abstraction that is device-independent ä 2 types: byte and character (Unicode)

java.io package ä Major players: ä InputStream ä OutputStream ä Reader ä Writer ä Others: ä File ä RandomAccessFile ä FileDescriptor ä ObjectStreamClass ä ObjectStreamField ä SerializablePermission ä SteamTokenizer byte stream characterstream(Unicode)

java.io package File class ä Used to access file and directory objects ä Uses file-naming conventions of host OS ä Some methods: ä getAbsolutePath( ) ä getParent( ) ä listFiles( ) ä isDirectory( ) ä isFile( ) ä Example: Using a File constructor ä File file = new File(“test.dat”); ä exists( ) ä getName( ) ä length( ) ä lastModified( ) ä canRead( )

java.io package InputStream OutputStream classes ä InputStream ä FilterInputStream ä BufferedInputStream ä DataInputStream ä LineNumberInputStream ä PushBackInputStream ä ByteArrayInputStream ä FileInputStream ä ObjectInputStream ä PipedInputStream ä SequenceInputStream ä StringBufferInputStream ä OutputStream ä FilterOutputStream ä BufferedOutputStream ä DataOutputStream ä PrintStream ä ByteArrayOutputStream ä FileOutputStream ä ObjectOutputStream ä PipedOutputStream

Chaining Streams FileOutputStreamDataOutputStream File file = new File("test.txt"); FileOutputStream outFile = new FileOutputStream(file); DataOutputStream outData = new DataOutputStream(outFile); The FileOutputStream class enables output (stream of bytes) to be written to a file stream. The DataOutputStream class is an output filter that allows Strings and primitive data types to be written to a file stream. Input Stream Output Stream

Combining Filters Filter 1 InputStreamOutput Stream Stream Filter 2 Filter 3 Filter 4

File I/O Exception Handling ä Try, catch, throw paradigm (remember from C++?) ä Some types of exceptions: ä IOException when operating on file ä ClassNotFoundException during reading/deserialization ä Example: try{ output.close( );} catch (IOException ioex) { JOptionPane.showMessageDialog(null, “Error closing file”, “Error”, JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, “Error closing file”, “Error”, JOptionPane.ERROR_MESSAGE);}

File I/O Example (see FileTest.java code on class Web site)

Serializing a Java Object ä Serializing: Converting object to a format suitable for stream I/O ä Deserializing: Converting serialized object back into object instance. ä To serialize, class must implement java.io. Serializable interface ä Serialized object contains part of state of an instance: ä identifying info for class ä instance variables ä all reachable non-transient and non-static objects ä not the methods!

Serializing a Java Object (continued) ä Why? ä File I/O: Object “data” persistence ä JavaBean: reusable software component ä Remote Method Invocation (RMI) ä Jini

Serializing a Java Object (continued) ä How? ä To serialize an object, its class must implement the Serializable interface ä Example: ä public class Point2D extends Object implements Serializable {...} {...} ä To write/read an object, chain ObjectOutputStream FileOutputStreamObjectOutputStream Input Stream Output Stream

Serializing Example (see FileTest2.java code on class Web site)

Web/Networking Example (see Deitel Figure 20.2 of Chapter 20)