Java IO Exploring the java.io package and living to talk about it.

Slides:



Advertisements
Similar presentations
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.
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.
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.
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.
Streams and Files The objectives of this chapter are: To understand the principles of I/O streams and where to use them To understand the options and limitations.
Java I/O and Java Networking (Client Side) Yoshi.
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.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
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
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Lecture 7 Exceptions and I/O. Overview of the exception hierarchy A simplified diagram of the exception hierarchy in Java Throwable ErrorException IOException.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
CSC – Java Programming II Lecture 9 January 30, 2002.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
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 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.
Copyright © Curt Hill Java I/O Flexibility and Layering.
I / O in java. java.io BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
The Java I/O Classes and Interfaces cont’d
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.
A stream is a sequence of data. A stream is a flowing sequence of characters.
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.
Java IO. Why IO ? Without I/O, your program is a closed box. Without I/O, your program is a closed box. I/O gives your Java program access to your hard.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
– 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.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
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.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Chapter 10: I/O Streams Input Streams Output Streams
Fundamental of Java Programming
Sequential files creation & writing
Files and Streams The material in this chapter is not tested on the AP CS exams.
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
IO in java.
Exception Handling, Reading and Writing in Files, Serialization,
OO Design and Programming II I/O: Reading and Writing
Ch14 Files and Streams OBJECTIVES
Chapter 8: 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.
Java Methods ("Chapter %d", 14); Streams and Files A & AB
File I/O & collection frame work
Programming in Java Files and I/O Streams
Објектно орјентисано програмирање
Java’s Hierarchy Input/Output Classes and Their Purpose
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Chapter 12 File Input and Output
Stream Oriented I/O Computer Programming II Dr. Tim Margush
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Files and Streams in Java
CS 240 – Advanced Programming Concepts
David Davenport Spring 2005
Presentation transcript:

Java IO Exploring the java.io package and living to talk about it

Streams A stream is a sequence of data of undetermined length. Byte oriented and numeric data is written with output streams and read with input streams Text or character data is written with writers and read with … readers

Four Main Java classes Java.io.InputStream Java.io.OutputStream Java.io Reader Java.io.writer All are abstract classes with many subclasses.

Streams A stream is composed of discreet bytes, containing anything Processed best with a while loop looking for an end of stream marker Windows Ctrl-Z Unix Ctrl X

Subclasses BufferedInputStream ByteArrayInputStream DataInputStream FileInputStream FilterInputStream LineNumberInputStream ObjectInputStream PipedInputStream PrintStream PushbackInputStream SequenceInputStream StringBufferInputStream BufferedOutputStream ByteArrayOutputStream DataOutputStream FileOutputStream FilterOutputStream ObjectOutputStream PipedInputStream PipedOutputStream

Reading bytes of data The basic read() method of the InputStream class reads a single unsigned byte of data and returns the int value of the unsigned byte. This is a number between 0 and 255. If the end of stream is encountered it returns -1 instead, and you can use this as a flag to watch for the end of stream. public abstract int read() throws IOException

import java.io.*; public class Echo { public static void main(String[] args) { echo(System.in); } public static void echo(InputStream in) { try { while (true) { // Notice that although a byte is read, an int // with value between 0 and 255 is returned. // Then this is converted to an ISO Latin-1 char // in the same range before being printed. int i = in.read(); // -1 is returned to indicate the end of stream if (i == -1) break; // without the cast a numeric string like "65" // would be printed instead of the character "A" char c = (char) i; System.out.print(c); } } catch (IOException e) { System.err.println(e); } System.out.println(); }

Buffered Streams The java.io.BufferedInputStream and java.io.BufferedOutputStream classes buffer reads and writes by first storing the in a buffer (an internal array of bytes). Then the program reads bytes from the stream without calling the underlying native method until the buffer is empty. The data is read from or written into the buffer in blocks; subsequent accesses go straight to the buffer.

Readers and Writers The java.io.Reader and java.io.Writer classes are abstract superclasses for classes that read and write character based data. The subclasses are notable for handling the conversion between different character sets.

The Difference Input and output streams are fundamentally byte based. However readers and writers are based on characters, which can have varying widths depending on the character set being used. For example, ASCII and ISO Latin-1 use one byte characters. Unicode uses two byte characters. UTF-8 uses characters of varying width between one and three bytes. Readers and writers know how to handle all these character sets and many more seamlessly.

java.io.Reader class The methods of the java.io.Reader class are deliberately similar to the methods of the java.io.InputStream class. –However rather than working with bytes, they work with chars. The basic read() method reads a single character (which may may take between one and four bytes depending on the character set) and returns the character as an int between 0 and It returns -1 if the end of stream is seen.

The java.io.Writer class The methods of the java.io.Writer class are deliberately similar to the methods of the java.io.OutputStream class. –However rather than working with bytes, they work with chars. The basic write() method writes a single two-byte character with a value between 0 and

The InputStreamReader Class The java.io.InputStreamReader class serves as a bridge between byte streams and character streams: It reads bytes from the input stream and translates them into characters according to a specified character encoding.

More Java IO Files

File Class File class is a general machine independent interface to the file system. File class is not for the contents of a file, but the file object –Directories are File Objects in java Several methods are available with this class.

File Class Methods getName() getPath() getAbsolutePath() getParent() isAbsolute() lastModified() IsFile() isDirectory() canRead() canWrite()

Directory entries String[] list() File[] listFiles() Both can use FileFilter interface –Boolean accept(File pathname)

File Streams FileInputStream and FileOutputStream –Byte –Reads and writes data as sequence of Bytes File Writers and Readers Char –Reads and writes data as sequence Unicode characters

Random Access Files Random access files can be read from or written to or both from a particular byte position in the file. The position in the file is indicated by a file pointer. There are two constructors in this class: public RandomAccessFile(String name, String mode) throws IOException public RandomAccessFile(File file, String mode) throws IOException

The first argument to the constructor is the file you want to access. The second argument is the mode for access. This should either be the String "r" for read-only access or the string "rw" for read and write access. Java does not support write only access. For example, RandomAccessFile raf = new RandomAccessFile("29.html", "r");

The getFilePointer(), length(), and seek() methods allow you to determine and modify the point in the file at which reads and writes occur. Attempts to seek (position the file pointer) past the end of the file just move the file pointer to the end of the file. Attempts to write from the end of the file extend the file. Attempts to read from the end of the file throw EOFExceptions.

public long getFilePointer() throws IOException public void seek(long pos) throws IOException public long length() throws IOException

Reads and writes use methods that work identically to the methods of the DataInputStream and DataOutputStream classes, except that you can set the position at which the read or write occurs between calls to the read and write methods.