Object-Oriented Programming Design. Topic : Streams and Files

Similar documents
Darshan Institute of Engineering & Technology for Diploma Studies

Chapter 10. IO Streams

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

CS 251 Intermediate Programming Java I/O Streams

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Lecture 11.1 I/O Streams

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

PIC 20A Streams and I/O

Performing input and output operations using a Byte Stream

Java Input / Output. CSE 413, Autumn 2002 Programming Languages.

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

IT101. File Input and Output

I/O Streams. Object-oriented programming

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

The Java I/O System. Binary I/O streams (ASCII, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

1.00 Lecture 30. Sending information to a Java program

Byte and Character Streams. Reading and Writing Console input and output

Objec&ves. Review. Standard Error Streams

I/O STREAM (REQUIRED IN THE FINAL)

Stream Manipulation. Lecture 11

Example: Copying the contents of a file

CSC 1214: Object-Oriented Programming

Software 1. Java I/O

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Objec&ves STANDARD ERROR. Standard Error Streams. Ø Byte Streams Ø Text Streams 10/5/16. Oct 5, 2016 Sprenkle - CSCI209 1

File Operations in Java. File handling in java enables to read data from and write data to files

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Programming Languages and Techniques (CIS120)

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

Programming Languages and Techniques (CIS120)

Software Practice 1 - File I/O

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

Exceptions and Working with Files

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Each command-line argument is placed in the args array that is passed to the static main method as below :

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Dining philosophers (cont)

CS193j, Stanford Handout #26. Files and Streams

pre-emptive non pre-emptive

Input from Files. Buffered Reader

Data Structures. 03 Streams & File I/O

Files and IO, Streams. JAVA Standard Edition

Special error return Constructors do not have a return value What if method uses the full range of the return type?

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 10 Input Output Streams

Programming Languages and Techniques (CIS120)

Software 1. תרגול 9 Java I/O

Simple Java Input/Output

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output

Experiment No: Group B_4

Java Input/Output. 11 April 2013 OSU CSE 1

Course Content. Objectives of Lecture 22 File Input/Output. Outline of Lecture 22. CMPUT 102: File Input/Output Dr. Osmar R.

Java Input/Output Streams

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Software 1 with Java. The java.io package. Streams. Streams. Streams. InputStreams

CSB541 Network Programming 網路程式設計. Ch.2 Streams 吳俊興國立高雄大學資訊工程學系

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

Algorithms. Produced by. Eamonn de Leastar

CS Week 11. Jim Williams, PhD

CS 200 File Input and Output Jim Williams, PhD

HST 952. Computing for Biomedical Scientists Lecture 8

Programming Languages and Techniques (CIS120)

Remedial Java - io 8/09/16. (remedial) Java. I/O. Anastasia Bezerianos 1

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

Programming Languages and Techniques (CIS120)

C17: I/O Streams and File I/O

The I/O Package. THE Java platform includes a number of packages that are concerned with the CHAPTER20

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

I/O streams. Byte Streams Character Streams InputStream ByteArrayInputStream FileInputStream FilterInputStream

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

Here is a hierarchy of classes to deal with Input and Output streams.

File Processing in Java

CPSC 319. Week 2 Java Basics. Xiaoyang Liu & Sorting Algorithms

输 入输出相关类图. DataInput. DataOutput. java.lang.object. FileInputStream. FilterInputStream. FilterInputStream. FileOutputStream

Lecture 4: Exceptions. I/O

Programmierpraktikum

Core Java Contents. Duration: 25 Hours (1 Month)

Chettinad College of Engineering & Technology Department of Information Technology Internal Examination II (Answer Key)

Lesson 3: Accepting User Input and Using Different Methods for Output

CISC 323 (Week 9) Design of a Weather Program & Java File I/O

CN208 Introduction to Computer Programming

Princeton University COS 333: Advanced Programming Techniques A Subset of Java

1993: renamed "Java"; use in a browser instead of a microwave : Sun sues Microsoft multiple times over Java

Lecture 7. File Processing

Object-Oriented Programming in the Java language

Internet Technology 2/7/2013

needs to be reliable, easy to change, retarget efficiency is secondary implemented as interpreter, with virtual machine

FILE I/O IN JAVA. Prof. Chris Jermaine

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

CSD Univ. of Crete Fall Files, Streams, Filters

CS 2113 Software Engineering

CS September 2017

ITI Introduction to Computer Science II

Transcription:

Electrical and Computer Engineering Object-Oriented Topic : Streams and Files Maj Joel Young Joel Young@afit.edu. 18-Sep-03 Maj Joel Young

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) Source Sink programs, user input, etc. files, console, sockets, etc. 2

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 Source Processor Sink 3

Data Sinks/Sources Several kinds of data streams in Java File Reader File Writer Pipe Reader Processor(s) Pipe Writer Processor(s) String Reader Char Array Reader String Writer Char Array Writer Input Streams (Application is Sink) Output Streams (Application is Source) 4

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 double, int, char, String, etc. Data OutputStream bytes Buffered OutputStream bytes File OutputStream bytes Hard Disk 5

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 double, int, char, String, etc. Data InputStream bytes Buffered InputStream bytes File InputStream bytes Hard Disk 6

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(); 7

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(); 8

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(); 9

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(); 10

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) System.out.println(line); line = br.readline(); br.close(); // Any more text to read? // Print the text to the console // Read more text 11

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(); 1 4.0 Smith, Joe 2 2.8 Jones, Jim Bob 3 3.9 Doe, Tricia... 12

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 13

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 14