David Davenport Spring 2005

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

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.
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.
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Lecture 15: I/O and Parsing
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.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
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.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
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.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
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/
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
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.
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.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
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.
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.
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.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
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.
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.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
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 I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
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.
java.io supports console and file I/O
The Java IO System Different kinds of IO Different kinds of operations
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
IO in java.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
CSG2H3 Object Oriented Programming
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
12: The Java I/O System stream model.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Chapter 17 Binary I/O 1.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Chapter 17 Binary I/O Dr. Clincy - Lecture.
JAVA IO.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Chapter 17 – Files and Streams
CSC 143 Java Streams.
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Files and Streams in Java
Web Design & Development Lecture 8
Java Basics Introduction to Streams.
Streams and Readers The stream hierarchy is for reading bytes, the reader/writer hierarchy is for reading characters Unicode characters can be used internationally,
Presentation transcript:

David Davenport Spring 2005 CS102 – Files & Streams David Davenport Spring 2005

Input / Output in Java In java.io package File class refers to individual file/folder (but does not do I/O) Java abstracts I/O to Streams Sequential IO (inc. files) Can only read or write (not modify) Character vs. Byte (Text vs. Binary) RandomAccessFile File class gives info about a file or folder. Eg. Is it read only, is it a file or a folder, if a folder what files does it contain, last modified, etc.

Sequential I/O Reading from a stream… Writing to a stream… open stream to read from while stream contains more data read & process some data from stream close stream Writing to a stream… open stream to write to // destroys any existing data! while there is more data to write write data to stream close stream // may flush first! Standard streams: in, out, err In, out, err normally “connected” to keyboard and console but can be redirected (as in StdIO class.) In is an InputStream (probably actually a DataInputStream object) & out/err are PrintStream Normally opening a file to write to destroys all existing data, but FileWriter also allows append mode.

Character Streams For Unicode “Text” I/O FileReader & FileWriter classes read/write Unicode InputStreamReader & OutputStreamWriter classes read/write native 8-bit encoded files! Only char/String, use PrintWriter for any type Use Buffered classes for efficiency As far as I understand, these streams are purely Unicode and only read/write char & Strings. The InputStreamReader & OutputStreamWriter classes provide conversion from/to native 8 bit encoding of local machine. PrintWriter will allow any type (primitive too) to be read/written.

Example – Character Streams FileWriter file = new FileWriter( “temp.txt”); PrintWriter output = new PrintWriter( file); output.println( “This is some text”); output.println( 27.3); output.println( True); output.close(); FileReader file = new FileReader( “temp.txt”); BufferedReader input = new BufferedReader ( file); String aLine = input.readLine(); while ( aLine != null ) { System.out.println( aLine); aLine = input.readLine(); } input.close(); Note: without using PrintWriter you could only write String or char’s not boolean, double, etc. BufferedReader cannot read other data types! Also, it does not produce exceptions when reaching end of file!

Byte Streams For binary I/O Generally use DataInputStream & DataOutputStream, with FileInoutStream & FileOutputStream classes Use Buffered classes for efficiency use PrintStream to convert to text output ObjectInputStream & ObjectOutputStream provide Object serialization readLine() in DataInputStream…           Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method.

Example – Binary Streams FileOutputStream file = new FileOutputStream( “temp.txt”); BufferedOutputStream boutput = new BufferedOutputStream ( file); DataOutputStream output = new DataOutputStream ( boutput); output.writeUTF( “This is some text”); output.writeDouble( 27.3); output.writeBoolean( True); output.close(); FileInputStream file = new FileInputStream( “temp.txt”); BufferedInputStream binput = new BufferedInputStream ( file); DataInputStream input = new DataInputStream( binput); String s = input.readUTF(); double d = input.readDouble(); boolean b = input.readBoolean(); System.out.println( s + “ ” d + “ ” + b ); input.close();

RandomAccessFiles Can read & write (i.e. modify) files Can seek (go to) any byte in file Bytes numbered from zero Need to be aware of data & size! (everything is just 1’s & 0’s)

Example – RandomAccessFiles // Demo append to file using Java RandomAccessFile import java.io.*; public class RandomAccessFileDemo { public static void appendTo( String fileName ) throws IOException { System.out.println("--------------------------"); RandomAccessFile f = new RandomAccessFile( fileName, "rw"); f.seek( f.length() ); f.writeBytes( "This should be appended to the file, I hope!\r\n"); f.close(); System.out.println("--------------------------\n"); DataInputStream ff = new DataInputStream( new FileInputStream( fileName)); int c = ff.read(); while (c != -1) { System.out.println( c ); c = ff.read(); } ff.close(); public static void main( String[] args) throws IOException { try { appendTo( "appendtest.dat"); System.out.println( " .. Done .. "); } catch ( Exception e) { System.out.println(e); System.in.read();

ToDo OUTPUT streams MUST be closed else may lose data! add few notes about different files types (sequential and Random Access) and about different Streams (byte vs. character) byte streams are byte oriented, character streams are unicode and about data vs processed source can be file, string, etc. and passed raw or filtered/converted Readers/Writers are unicode based. Standard IO streams… in, out, err. Serialization