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.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 3. 2 Handling Primitive Data Types The basic input and output streams provide read/write methods that can be used.
Advertisements

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.
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.
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.
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.
Files Files are used to store long term data. –Typically referred to as persistent data. –A file is a group of related records. –A list of friends addresses.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
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.
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.
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.
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.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 19 Binary I/O.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Use JFileChooser F Text Input.
CIS 270—Application Development II Chapter 14—Files and Streams.
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.
Chapter 17 Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams  Use JFileChooser F Text Input.
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: 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.
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.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
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.
Two Ways to Store Data in a File  Text format  Binary format.
© Amir Kirsh Files and Streams in Java Written by Amir Kirsh.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Binary I/O.
PROG Mobile Java Application Development PROG Mobile Java Application Development Data Storage, Continued.
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 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Parsing Text Files F Random.
Chapter 15: Input and Output
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
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.
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.
Java Programming, Second Edition Chapter Sixteen File Input and Output.
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
The Java IO System Different kinds of IO Different kinds of operations
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
CSG2H3 Object Oriented Programming
IO in java.
Chapter 17 Input and Output
Chapter 17 Binary I/O.
Object Writing in files
OBJECT ORIENTED PROGRAMMING II LECTURE 21_1 GEORGE KOUTSOGIANNAKIS
I/O Basics.
Chapter 17 Binary I/O 1.
File.
Streams and File I/O Chapter 9 Chapter 9.
Java Programming Course
Chapter 17 Binary I/O Dr. Clincy - Lecture.
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Java Basics Introduction to Streams.
David Davenport Spring 2005
Presentation transcript:

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 in a multithreaded environment Daemon Thread #1 Main Thread #2 Local Memory Global Memory Your Thread #3 Local Memory

Objectives 3

Java Input Output(IO) is one of the most important topics in Java

A stream is an ordered sequence of bytes of indeterminate length. A data stream is a channel through which data travels from a source to a destination There are two type of stream: Input Stream Output Stream

The Stream classes in Java's I/O libraries are designed in an abstract way that enables you to read from external data sources and write to external targets. Reading and writing without caring where your data is coming from or where it's going is a very powerful abstraction. For example: You use the same methods to read from a file that you do to read from a console, a network connection, a serial port device etc..

1. Open an input/output stream associated with a physical device (e.g., file, network, console/keyboard), by constructing an appropriate IO-stream object. 2. Read from the opened input stream until "end-of-stream" encountered, or Write to the opened output stream (optionally flush the buffered output). 3. Close the input/output stream. IO operations involved three steps:

Java's IO operations is more complicated than C/C++ Java uses 16-bit character set (instead of 8-bit character set). As a consequence, it needs to differentiate between byte-based IO and character-based IO.

Byte-Based IO & Byte Streams

Character-Based IO & Character Streams

Reading bytes from a binary stream and convert the data to any of the Java primitive types Convert data from Java modified Unicode Transmission Format (UTF) - 8 format into string form Methods: readBoolean() readByte() readInt() readDouble() readChar() readLine() and readUTF()

Convert data present in Java primitive type into a series of bytes and write them onto a binary stream Convert string data from into Java modified UTF-8 format and write it into a stream Methods: writeBoolean() writeByte() writeInt() writeDouble() writeChar() writeChars() and writeUTF()

InputStream class is an abstract class that defines how streams receive data and is the superclass of all stream classes Methods: read(), available(), close(), mark(), skip() and reset()

FileInputStream class is used to read bytes from a file. FileInputStream class overrides all the methods of the InputStream class except mark() and reset(). ByteArrayInputStream class contains a buffer (a byte array) that stores the bytes that are read from the stream.

OutputStream class is an abstract class that defines the method in which bytes or array of bytes are written to streams Methods: write(), flush(), close()…

File class directly works with files and directories. Some common methods:

FileDescriptor class provides access to the file descriptors that are maintained by the OS when files and directories are being accessed In practical use, a file descriptor is used to create a FileInputStream or FileOutputStream. Public fields: err, in and out

File file = new File(filename"); FileInputStream in=new FileInputStream(file); File file = new File(filename"); FileInputStream in=new FileInputStream(file); File file = new File(filename"); FileOutputStream out=new FileOutputStream(file); File file = new File(filename"); FileOutputStream out=new FileOutputStream(file); Open a file: int byteRead=0; while (byteRead!=-1) { byteRead=in.read(); } int byteRead=0; while (byteRead!=-1) { byteRead=in.read(); } Read data: Save data to a file: int byteRead=0; while (byteRead!=-1) { byteRead=in.read(); out.write(byteRead); } int byteRead=0; while (byteRead!=-1) { byteRead=in.read(); out.write(byteRead); } Close all: try{ //Do Open, Read, Write data }catch(IOException ex) { ex.printStackTrace(); }finally { try { if (in != null) in.close(); if (out != null) out.close(); } catch (IOException ex) { ex.printStackTrace(); } try{ //Do Open, Read, Write data }catch(IOException ex) { ex.printStackTrace(); }finally { try { if (in != null) in.close(); if (out != null) out.close(); } catch (IOException ex) { ex.printStackTrace(); }

A buffer is a temporary storage area for data. By storing the data in a buffer, time is saved as data is immediately received from the buffer instead of going back to the original source of the data.(Demo)

FileInputStream fin = new FileInputStream("in.dat"); BufferedInputStream bin = new BufferedInputStream(fin); DataInputStream din = new DataInputStream(bin); // or DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("in.dat"))); FileInputStream fin = new FileInputStream("in.dat"); BufferedInputStream bin = new BufferedInputStream(fin); DataInputStream din = new DataInputStream(bin); // or DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("in.dat")));

Character-Based IO & Character Streams

Reader class is an abstract class used for reading character streams. Writer class is an abstract class and supports writing characters into streams. PrintWriter class is a character-based class that is useful for console output. This class provides support for Unicode characters.

Serialization is the process of reading and writing objects to a byte stream ObjectInputStream class extends the InputStream class and implements the ObjectInput interface ObjectInput interface extends DataInput interface and has methods that support object serialization ObjectOutputStream class extends the OutputStream and implements the ObjectOutput interface Important methods: ObjectInputStream.readObject(), ObjectOutputStream.writeObject()

ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream( new FileOutputStream("object.dat"))); out.writeObject("The current Date and Time is "); // write a String object out.writeObject(new Date()); // write a Date object out.flush(); out.close(); ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream( new FileOutputStream("object.dat"))); out.writeObject("The current Date and Time is "); // write a String object out.writeObject(new Date()); // write a Date object out.flush(); out.close(); ObjectInputStream out = new ObjectInputStream( new BufferedInputStream( new FileInputStream("object.dat"))); String str = (String)in.readObject(); Date d = (Date)in.readObject(new Date()); in.close(); ObjectInputStream out = new ObjectInputStream( new BufferedInputStream( new FileInputStream("object.dat"))); String str = (String)in.readObject(); Date d = (Date)in.readObject(new Date()); in.close(); Save serialized objects to a stream: Load serialized objects from a stream:

In Java, object that requires to be serialized must implement java.io.Serializable or java.io.Externalizable interface Serializable interface is an empty interface (or tagged interface) with nothing declared Its purpose is simply to declare that particular object is serializable.

Character - base Byte- base Stream Data Source Input Stream Output Stream Java Program

Java I/O, 2nd Edition By Elliotte Rusty HaroldBy Elliotte Rusty Harold and_file_Handling and_file_Handling al/io/streams.html al/io/streams.html mming/java/J5b_IO.html mming/java/J5b_IO.html