5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

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.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
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.
Java I/O The Cheat Sheet. Reading and Writing The basic architecture of Java IO is pluggable. The idea is to have some very simple classes do very simple.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
CS-2852 Data Structures LECTURE 1B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
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.
Java I/O and Java Networking (Client Side) Yoshi.
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
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 ->
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/
Two Ways to Store Data in a File Text format Binary format.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
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
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.
MIT AITI 2003 Lecture 15 Streams Input and Output data from/to other sources.
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
File IO Basics By Dan Fleck Coming up: Data Streams.
Two Ways to Store Data in a File  Text format  Binary format.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
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.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
1 OOP Lecture 17 I/O and Graphics Signe Ellegård Borch Carsten Schuermann IT University Copenhagen.
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
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.
Chapter 15: Input and Output
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
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 and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
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.
The Java IO System Different kinds of IO Different kinds of operations
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Exception Handling, Reading and Writing in Files, Serialization,
12: The Java I/O System stream model.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
File.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
I/O Streams- Basics Byte Streams and Character Streams
JAVA IO.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
CSC 143 Java Streams.
Files and Streams in Java
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Web Design & Development Lecture 8
CS 240 – Advanced Programming Concepts
EEC 484/584 Computer Networks
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
David Davenport Spring 2005
Presentation transcript:

5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young Joel Maj Joel Young

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-152 Object-Oriented Programming Design Java Input/Output Java implements input/output in terms of streams of characters –Streams have a producer, or source –Streams have a consumer, or sink –Sometimes sources are sinks depending on use (a file can be both a source and a sink) SourceSink programs, user input, etc. files, console, sockets, etc.

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-153 Object-Oriented Programming Design I/O Processors Control over the stream is improved by adding processors – Convert char streams to integers, doubles, strings, etc. – Perform filtering – Buffer characters to improve read performance SourceSink Processor

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-154 Object-Oriented Programming Design Data Sinks/Sources Several kinds of data streams in Java File Reader Pipe Reader Processor(s) String Reader Char Array Reader Processor(s) File Writer Pipe Writer String Writer Char Array Writer Input Streams (Application is Sink) Output Streams (Application is Source)

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-155 Object-Oriented Programming Design The “Pipeline” Concept Writing Data: Start with a sink, such as a FileOutputStream Writes only one byte at a time (or an array of bytes), not very efficient – so we add a buffer manager Can still only write a byte (or array of bytes), so we add a DataOutputStream to allow more complex types Data OutputStream Data OutputStream double, int, char, String, etc. Buffered OutputStream Buffered OutputStream bytes Hard Disk Hard Disk File OutputStream File OutputStream bytes

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-156 Object-Oriented Programming Design The “Pipeline” Concept Reading Data: Start with a source, such as a FileInputStream Reads only one byte at a time (or an array of bytes), not very efficient – so we add a buffer manager Can still only read a byte (or array of bytes), so we add a DataInputStream to allow more complex types Data InputStream Data InputStream double, int, char, String, etc. Buffered InputStream Buffered InputStream bytes Hard Disk Hard Disk File InputStream File InputStream bytes

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-157 Object-Oriented Programming Design File Streams File Streams (Byte Stream Classes) – FileInputStream: read bytes/arrays of bytes – FileOutputStream: write bytes/arrays of bytes import java.io.*; public class Copy { public static void main(String[] args) throws IOException { FileInputStream in = new FileInputStream(“source.dat”); FileOutputStream out = new FileOutputStream(“dest.dat”); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); }

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-158 Object-Oriented Programming Design File Streams Add DataInputStream/DataOutputStream – Provides read/write methods for primitives – Provides read/write methods for unicode strings import java.io.*; public class Test { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream(“output.dat”); DataOutputStream dos = new DataOutputStream(fos); dos.writeDouble(64.356); dos.close(); FileInputStream fis = new FileInputStream(“output.dat”); DataInputStream dis = new DataInputStream(fis); double test = dis.readDouble(); System.out.println( test ); dis.close(); }

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-159 Object-Oriented Programming Design File Streams Can keep adding processors... Add buffered input/output import java.io.*; public class Test2 { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream(“output.dat”); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); dos.writeDouble(64.356); dos.close(); FileInputStream fis = new FileInputStream(“output.dat”); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); double test = dis.readDouble(); System.out.println( test ); dis.close(); }

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-1510 Object-Oriented Programming Design File Streams File Streams (Character Stream Classes) – FileReader: read chars/arrays of chars – FileWriter: write chars/arrays of chars import java.io.*; public class Copy2 { public static void main(String[] args) throws IOException { FileReader in = new FileReader(“source.dat”); FileWriter out = new FileWriter(“dest.dat”); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); }

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-1511 Object-Oriented Programming Design ASCII Files Problem: ASCII uses 8-bit chars, but Java uses 16-bit Unicode chars – how do we read plain-text files? – InputStreamReader: Translates input bytes to Unicode public class Copy { public static void main(String[] args) throws IOException { FileInputStream in = new FileInputStream(“source.dat”); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr); String line = br.readLine(); // Read a line of text while (line != null) // Any more text to read? { System.out.println(line);// Print the text to the console line = br.readLine();// Read more text } br.close(); }

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-1512 Object-Oriented Programming Design ASCII Formatted Numbers & Variable Length Strings class Test { static public void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(“test.dat”))); String line = br.readLine(); // Get first line of text StringTokenizer st; int studentNum; double gradeAvg; String name; while (line != null) { st = new StringTokenizer(line); studentNum = Integer.parseInt(st.nextToken()); gradeAvg = Double.parseDouble(st.nextToken()); name = st.nextToken(); while (st.hasMoreTokens()) { name = name + “ “ + st.nextToken(); } System.out.println(studentNum+”,”+gradeAvg+”,”+name); line = br.readLine(); } Smith, Joe Jones, Jim Bob Doe, Tricia...

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-1513 Object-Oriented Programming Design Console I/O PrintStream – Heavily overloaded versions of print() for: – print(int n) – print(double d) – print(float f) – print(String s) – … – Version called println() puts a newline character after the formatted text

Air Force Institute of Technology Electrical and Computer Engineering 5-Oct-1514 Object-Oriented Programming Design System.in & System.out Like C++ keeps “stdin” and “stdout” streams open at all times – System.in is a class attribute of type InputStream – System.out is a class attribute of type PrintStream import java.io.*; public class Test { public static void main(String[] args) throws IOException { System.out.print(“Here’s a string: ”); System.out.println(10); // Integer 10 w/new line break }