Active Learning: Streams

Size: px
Start display at page:

Download "Active Learning: Streams"

Transcription

1 Lecture 29 Active Learning: Streams The Logger Application 2 1

2 Goals Using the framework of the Logger application, we are going to explore three ways to read and write data using Java streams: 1. as text 2. as binary data 3. as serialized objects We're going to learn how to use Java's built in LinkedList class and its helper class ListIterator to maintain the log. 3 Logger Architecture Logger.java: provides main() and GUI generates log data ("Log") as Date objects summarizes and performs minimal validity check ("Summarize"); selects Files for reading and writing using a JFileChooser calls methods in LoggerIO to perform reading and writing of data You are going to write those methods. 4 2

3 java.util.linkedlist java.util.linkedlist possesses the same methods as our SLinkedList and many others. Logger uses a LinkedList to hold the log data, and it passes the list in calls to the LoggerIO methods you will have to write. For instance: void savetotext( List list, File f ) 5 Iterators An iterator is a helper class designed to be used with a List or other collection class. It has methods to return the collection's members one at a time. Iterators can also implement methods that allow the collection to be modified relative to the current position of the iterator. 6 3

4 ListIterator Interface public interface ListIterator { public boolean hasnext(); public Object next() throws NoSuchElementException; public boolean hasprevious(); public Object previous() throws NoSuchElementException; public void remove() throws IllegalStateException; public void add( Object o ); public void set( Object o ) throws IllegalStateException.... } 7 Iterator Methods Java iterators return a new element and advance to the next with the same operation, next(). The Java ListIterator also allows you to go backwards ( hasprevious() and previous() ). The most recent element returned by next() is the current element. remove() will delete the current element from the underlying collection. set() will change it. add() will insert a new element after the current element and before the element that would be returned by the following call to next(). After a call to add(), the inserted element becomes the new current. A call to next() will return the element after the inserted one. The first call to next() should return the first element of the list. 8 4

5 Using an Iterator List mylist = new LinkedList();... ListIterator iter = mylist.listiterator();... while ( iter.hasnext() ) { Object o = iter.next();... } 9 Traditional I/O The traditional approach uses different schemes depending on the type of the source or destination, e.g., keyboard input screen output files interprocess pipes network sockets 10 5

6 Java I/O Java s preferred approach is to handle I/O using streams (pioneered in C++) Java provides set of abstract stream classes that define the stream interfaces hierarchy of stream implementations Java allows you to treat some I/O distinctively, e.g. RandomAccessFile 11 Input vs Output Streams Streams are unidirectional An input stream controls data coming into the program from some source An output stream controls data leaving the program for some destination If you want to read and write the same destination, you use 2 streams 12 6

7 Streams and I/O Channels Usually the other end of a stream leads to or arises from a platform-specific media service, for instance, a file system Input Stream Output Stream File System Program File System 13 What Streams Share Java Streams are FIFO queues Streams deliver information in the order it was inserted into the underlying channel Standard Java streams only provide sequential access without rewind, backup, or random access 14 7

8 Coupling Streams Java streams may be combined by using one stream as a constructor argument to another This is usually done to add functionality and/or convert the data Stream pipelines are constructed from the data source to the program or from the data destination back to the program 15 Stream Pipeline, I File Input Stream Input Stream Reader Buffered Reader Stream Tokenizer 16 8

9 Stream Pipeline, II A FileInputStream reads bytes from a file An InputStreamReader converts a byte stream to characters A BufferedReader buffers a character stream for efficiency A StreamTokenizer parses a character stream into tokens 17 Constructing the Pipeline FileInputStream f = new FileInputStream( path ); InputStreamReader i = new InputStreamReader( f ); BufferedReader b = new BufferedReader( i ); StreamTokenizer t = new StreamTokenizer( b ); 18 9

10 Logger Exercise Download the two files Logger.java and LoggerIO.java from the class web site (links from Lecture 29). Save them both to a new directory. Create a new project and mount the directory into which you just saved the two java files. Don't insert package declarations. Leave them in the default package. Compile the project and test it. Try to save to a file in text mode. Did you create a file? Was there anything in it? (Look at it with Notepad). 19 savetotext() We have given you almost the whole savetotext() method; get a sense of the pattern: The try/catch blocks How we construct the stream, a FileWriter in this case How we iterate down the list How we close the stream when we are done (closing the stream saves the file) Find the method in FileWriter to write the date String and use it to output nstring on writer. Test it. Look at what you wrote using Notepad

11 loadfromtext(), 1 Now proceed to the loadfromtext() method. Once again observe the pattern: Construct a BufferedReader from a FileReader; we need a BufferedReader to read lines of text at a time; Look at the while condition; we read the next line and check for EOF in one statement. Many input stream read methods return a special value when they reach the end of file. Understand why we use nested try/catch blocks and multiple catch clauses. Write a line of code to convert the line of text to a Date using Date parse( String s ) throws ParseException 21 loadfromtext(), 2 Write a second line of code to add the Date to the list the method returns. Get rid of the line (its only there to provide a dummy exception throw before you write the real code): if ( false ) throw new ParseException("",0); Test the code. Write data out, clear the logger, and see if you can load it back in in text mode. Use Notepad to hand write a new logger entry using the date pattern of the previous entries. See if you can read in the modified data. Check it with the Summarize command

12 savetodata(), 1 At the heart of the Date object is a private long data field, the number of milliseconds since 12:00 am 1 Jan savetodata() saves log times as longs instead of Strings. You can retrieve the long using the Date method long gettime(). You will need to construct a DataOutputStream in order to access methods that can write longs. You can't construct one directly from a File. You will need to construct a FileOutputStream first. 23 savetodata(), 2 UsesaveToText() as a model for the rest of the method. Remember to catch exceptions. Now test. Trying writing out the same set of log times as text and data in separate files. Look at both in Notepad. Check the length of both files (you can get an exact byte count by choosing Properties from the right button menu in Explorer)

13 loadfromdata(), 1 Write a while loop to read longs from the DataInputStream as long as there are any (check the documentation), to convert each to a Date (there is a Date(long) constructor), and to add the Date to the return list. Pay special attention to what happens when you run out of longs in the input stream. How can you exit the while loop? Compile and test. 25 loadfromdata(), 2 Make sure you can write log data out in Data mode, clear the logger, and read the same data back in. Can you create log data in this format using Notepad? Can you read data written in Data mode back in as text? Can you read Text mode data back in as data? Why? 26 13

14 savetoobject(), 1 In this method, we will write out the whole List of Dates as a single object using an ObjectOutputStream. Check the documentation on how to create an ObjectOutputStream and how to call the writeobject() method. This single call will write the list and everything on it. We will look at how this works in the next lecture. Compile and test. Write out some log data in Object mode and look at it in Notepad. Check the length of the file. How does it compare to Text and Data mode. 27 loadfromobject() Write the contents of the try/catch block to read the entire log list in using an ObjectInputStream. You will have to construct the stream, call readobject(), and then close the stream. Compile and test. Can you read back in a log list that you previously wrote out in Object mode? Can you read data in Text or Data mode back in using Object mode? What are the comparative advantages of each data mode? 28 14

1.00 Lecture 30. Sending information to a Java program

1.00 Lecture 30. Sending information to a Java program 1.00 Lecture 30 Input/Output Introduction to Streams Reading for next time: Big Java 15.5-15.7 Sending information to a Java program So far: use a GUI limited to specific interaction with user sometimes

More information

Files and Streams

Files and Streams Files and Streams 4-18-2006 1 Opening Discussion Do you have any questions about the quiz? What did we talk about last class? Do you have any questions about the assignment? What are files and why are

More information

CS 251 Intermediate Programming Java I/O Streams

CS 251 Intermediate Programming Java I/O Streams CS 251 Intermediate Programming Java I/O Streams Brooke Chenoweth University of New Mexico Spring 2018 Basic Input/Output I/O Streams mostly in java.io package File I/O mostly in java.nio.file package

More information

Class 26: Linked Lists

Class 26: Linked Lists Introduction to Computation and Problem Solving Class 26: Linked Lists Prof. Steven R. Lerman and Dr. V. Judson Harward 2 The Java Collection Classes The java.util package contains implementations of many

More information

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

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 9/e Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Data stored in variables and arrays is temporary It s lost when a local variable goes out of scope or when

More information

Chapter 12. File Input and Output. CS180-Recitation

Chapter 12. File Input and Output. CS180-Recitation Chapter 12 File Input and Output CS180-Recitation Reminders Exam2 Wed Nov 5th. 6:30 pm. Project6 Wed Nov 5th. 10:00 pm. Multitasking: The concurrent operation by one central processing unit of two or more

More information

Object-Oriented Programming in the Java language

Object-Oriented Programming in the Java language Object-Oriented Programming in the Java language Part 5. Exceptions. I/O in Java Yevhen Berkunskyi, NUoS eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Exceptions Exceptions in Java are objects. All

More information

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

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 Chapter 4 Java I/O X i a n g Z h a n g j a v a c o s e @ q q. c o m Content 2 Java I/O Introduction File and Directory Byte-stream and Character-stream Bridge between b-s and c-s Random Access File Standard

More information

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

Core Java Contents. Duration: 25 Hours (1 Month) Duration: 25 Hours (1 Month) Core Java Contents Java Introduction Java Versions Java Features Downloading and Installing Java Setup Java Environment Developing a Java Application at command prompt Java

More information

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Starting Out with Java: From Control Structures Through Objects Sixth Edition Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 11 I/O File Input and Output Reentering data all the time could get tedious for the user. The data can be saved to

More information

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading. Last Time Reviewed method overloading. A few useful Java classes: Other handy System class methods Wrapper classes String class StringTokenizer class Assn 3 posted. Announcements Final on June 14 or 15?

More information

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

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003 Input, Output and Exceptions COMS W1007 Introduction to Computer Science Christopher Conway 24 June 2003 Input vs. Output We define input and output from the perspective of the programmer. Input is data

More information

Exceptions and Working with Files

Exceptions and Working with Files Exceptions and Working with Files Creating your own Exceptions. You have a Party class that creates parties. It contains two fields, the name of the host and the number of guests. But you don t want to

More information

Training topic: OCPJP (Oracle certified professional Java programmer) or SCJP (Sun certified Java programmer) Content and Objectives

Training topic: OCPJP (Oracle certified professional Java programmer) or SCJP (Sun certified Java programmer) Content and Objectives Training topic: OCPJP (Oracle certified professional Java programmer) or SCJP (Sun certified Java programmer) Content and Objectives 1 Table of content TABLE OF CONTENT... 2 1. ABOUT OCPJP SCJP... 4 2.

More information

JAVA. Duration: 2 Months

JAVA. Duration: 2 Months JAVA Introduction to JAVA History of Java Working of Java Features of Java Download and install JDK JDK tools- javac, java, appletviewer Set path and how to run Java Program in Command Prompt JVM Byte

More information

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7) Software Development & Education Center Java Platform, Standard Edition 7 (JSE 7) Detailed Curriculum Getting Started What Is the Java Technology? Primary Goals of the Java Technology The Java Virtual

More information

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

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20 File IO Computer Science and Engineering College of Engineering The Ohio State University Lecture 20 I/O Package Overview Package java.io Core concept: streams Ordered sequences of data that have a source

More information

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

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת A Typical Program Most applications need to process some input and produce some output based on that input The Java IO package (java.io) is to make that possible

More information

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

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax Tirgul 1 Today s topics: Course s details and guidelines. Java reminders and additions: Packages Inner classes Command Line rguments Primitive and Reference Data Types Guidelines and overview of exercise

More information

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

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO) I/O and Scannar Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad Website: www.sisoft.in Email:info@sisoft.in Phone: +91-9999-283-283 I/O operations Three steps:

More information

CSCI 200 Lab 2 Inheritance, Polymorphism & Data Streams

CSCI 200 Lab 2 Inheritance, Polymorphism & Data Streams CSCI 200 Lab 2 Inheritance, Polymorphism & Data Streams In this lab you will write a set of simple Java interfaces and classes that use inheritance and polymorphism. You will also write code that uses

More information

Example: Copying the contents of a file

Example: Copying the contents of a file Administrivia Assignment #4 is due imminently Due Thursday April 8, 10:00pm no late assignments will be accepted Sign up in the front office for a demo time Dining Philosophers code is online www.cs.ubc.ca/~norm/211/2009w2/index.html

More information

Core Java Syllabus DAY -1 :

Core Java Syllabus DAY -1 : Core Java Syllabus DAY -1 : How to write Java Program Compiling and Executing java program Phases of java program Analysis of main() method What is JDK, JRE, JVM, JIT Features of Java DAY -2 : Identifiers

More information

Data Structures. 03 Streams & File I/O

Data Structures. 03 Streams & File I/O David Drohan Data Structures 03 Streams & File I/O JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN 0132162709 2012 Pearson Education, Inc., Upper Saddle River, NJ.

More information

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

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output COMP 213 Advanced Object-oriented Programming Lecture 19 Input/Output Input and Output A program that read no input and produced no output would be a very uninteresting and useless thing. Forms of input/output

More information

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004 1.00/1.001 Introduction to Computers and Engineering Problem Solving Final / December 13, 2004 Name: Email Address: TA: Section: You have 180 minutes to complete this exam. For coding questions, you do

More information

CS112 Lecture: Streams

CS112 Lecture: Streams CS112 Lecture: Streams Objectives: Last Revised March 30, 2006 1. To introduce the abstract notion of a stream 2. To introduce the java File, Input/OutputStream, and Reader/Writer abstractions 3. To show

More information

Object Oriented Programming CS104 LTPC:

Object Oriented Programming CS104 LTPC: Object Oriented Programming CS04 LTPC: 4-0-4-6 Instructor: Gauravkumarsingh Gaharwar Program: Bachelor of Computer Applications Class-Semester: FYBCA(Sem-II) Email: gauravsinghg@nuv.ac.in Phone Number:

More information

Core Java Syllabus. Overview

Core Java Syllabus. Overview Core Java Syllabus Overview Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java

More information

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved. Chapter 10 File I/O Copyright 2016 Pearson Inc. All rights reserved. Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program,

More information

File. Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices

File. Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices Java I/O File Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices Magnetic disks Optical disks Magnetic tapes Sequential

More information

CPS122 Lecture: Input-Output

CPS122 Lecture: Input-Output CPS122 Lecture: Input-Output Objectives: Last Revised April 3, 2017 1. To discuss IO to System.in/out/err 2. To introduce the abstract notion of a stream 3. To introduce the java File, Input/OutputStream,

More information

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004 1.00/1.001 Introduction to Computers and Engineering Problem Solving Final / December 13, 2004 Name: Email Address: TA: Solution Section: You have 180 minutes to complete this exam. For coding questions,

More information

Job Migration. Job Migration

Job Migration. Job Migration Job Migration The Job Migration subsystem must provide a mechanism for executable programs and data to be serialized and sent through the network to a remote node. At the remote node, the executable programs

More information

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

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output. CSE 143 Java Streams Reading: 19.1, Appendix A.2 GREAT IDEAS IN COMPUTER SCIENCE REPRESENTATION VS. RENDERING 4/28/2002 (c) University of Washington 09-1 4/28/2002 (c) University of Washington 09-2 Topics

More information

104. Intermediate Java Programming

104. Intermediate Java Programming 104. Intermediate Java Programming Version 6.0 This course teaches programming in the Java language -- i.e. the Java Standard Edition platform. It is intended for students with previous Java experience

More information

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

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi CSE 143 Overview Topics Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 2/3/2005 (c) 2001-5, University of Washington 12-1 2/3/2005 (c) 2001-5,

More information

CPS122 Lecture: Input-Output

CPS122 Lecture: Input-Output CPS122 Lecture: Input-Output Objectives: Last Revised January 19, 2010 1. To discuss IO to System.in/out/err 2. To introduce the abstract notion of a stream 3. To introduce the java File, Input/OutputStream,

More information

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

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O I/O 10-7-2013 I/O in Java I/O streams vs. Reader/Writer HW#3 due today Reading Assignment: Java tutorial on Basic I/O public class Swimmer implements Cloneable { public Date geteventdate() { return (Date)

More information

Complete Java Contents

Complete Java Contents Complete Java Contents Duration: 60 Hours (2.5 Months) Core Java (Duration: 25 Hours (1 Month)) Java Introduction Java Versions Java Features Downloading and Installing Java Setup Java Environment Developing

More information

PIC 20A Streams and I/O

PIC 20A Streams and I/O PIC 20A Streams and I/O Ernest Ryu UCLA Mathematics Last edited: December 7, 2017 Why streams? Often, you want to do I/O without paying attention to where you are reading from or writing to. You can read

More information

Lecture 7. File Processing

Lecture 7. File Processing Lecture 7 File Processing 1 Data (i.e., numbers and strings) stored in variables, arrays, and objects are temporary. They are lost when the program terminates. To permanently store the data created in

More information

JAVA. 1. Introduction to JAVA

JAVA. 1. Introduction to JAVA JAVA 1. Introduction to JAVA History of Java Difference between Java and other programming languages. Features of Java Working of Java Language Fundamentals o Tokens o Identifiers o Literals o Keywords

More information

Darshan Institute of Engineering & Technology for Diploma Studies

Darshan Institute of Engineering & Technology for Diploma Studies Streams A stream is a sequence of data. In Java a stream is composed of bytes. In java, 3 streams are created for us automatically. 1. System.out : standard output stream 2. System.in : standard input

More information

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

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 4/27/2004 (c) 2001-4, University of

More information

HST 952. Computing for Biomedical Scientists Lecture 8

HST 952. Computing for Biomedical Scientists Lecture 8 Harvard-MIT Division of Health Sciences and Technology HST.952: Computing for Biomedical Scientists HST 952 Computing for Biomedical Scientists Lecture 8 Outline Vectors Streams, Input, and Output in Java

More information

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. Representation of Primitive Java Types. CSE143 Au Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Sec. 19.1, Appendix A2 11/2/2003 (c) 2001-3,

More information

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Sec. 19.1, Appendix A2 11/2/2003 (c) 2001-3,

More information

1.00 Lecture 31. Streams 2. Reading for next time: Big Java , The 3 Flavors of Streams

1.00 Lecture 31. Streams 2. Reading for next time: Big Java , The 3 Flavors of Streams 1.00 Lecture 31 Streams 2 Reading for next time: Big Java 18.6-18.8, 20.1-20.4 The 3 Flavors of Streams In Java, you can read and write data to a file: as text using FileReader and FileWriter as binary

More information

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

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs Ananda Gunawardena Java APIs Think Java API (Application Programming Interface) as a super dictionary of the Java language. It has a list of all Java packages, classes, and interfaces; along with all of

More information

Chapter 10. IO Streams

Chapter 10. IO Streams Chapter 10 IO Streams Java I/O The Basics Java I/O is based around the concept of a stream Ordered sequence of information (bytes) coming from a source, or going to a sink Simplest stream reads/writes

More information

Chapter 11: Exceptions and Advanced File I/O

Chapter 11: Exceptions and Advanced File I/O Chapter 11: Exceptions and Advanced File I/O Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 11 discusses the following main topics:

More information

Question 1. (2 points) What is the difference between a stream and a file?

Question 1. (2 points) What is the difference between a stream and a file? CSE 143 Sp03 Midterm 2 Page 1 of 7 Question 1. (2 points) What is the difference between a stream and a file? Question 2. (2 points) Suppose we are writing an online dictionary application. Given a word

More information

Chapter 12: Exceptions and Advanced File I/O

Chapter 12: Exceptions and Advanced File I/O Chapter 12: Exceptions and Advanced File I/O Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All

More information

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE CSE 143 Overview Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 10/20/2004 (c) 2001-4, University of

More information

Streams and File I/O

Streams and File I/O Walter Savitch Frank M. Carrano Streams and File I/O Chapter 10 Objectives Describe the concept of an I/O stream Explain the difference between text and binary files Save data, including objects, in a

More information

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Lab 5: Java IO 12:00 PM, Feb 21, 2018 CS18 Integrated Introduction to Computer Science Fisler, Nelson Contents Lab 5: Java IO 12:00 PM, Feb 21, 2018 1 The Java IO Library 1 2 Program Arguments 2 3 Readers, Writers, and Buffers 2 3.1 Buffering

More information

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

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166 Lecture 22 Java Input/Output (I/O) Streams Dr. Martin O Connor CA166 www.computing.dcu.ie/~moconnor Topics I/O Streams Writing to a Stream Byte Streams Character Streams Line-Oriented I/O Buffered I/O

More information

I/O Streams. Object-oriented programming

I/O Streams. Object-oriented programming I/O Streams Object-oriented programming Outline Concepts of Data Streams Streams and Files File class Text file Binary file (primitive data, object) Readings: GT, Ch. 12 I/O Streams 2 Data streams Ultimately,

More information

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

The Java I/O System. Binary I/O streams (ASCII, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits) The Java I/O System Binary I/O streams (ASCII, 8 bits) InputStream OutputStream The decorator design pattern Character I/O streams (Unicode, 16 bits) Reader Writer Comparing binary I/O to character I/O

More information

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

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O Week 12 Streams and File I/O Overview of Streams and File I/O Text File I/O 1 I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or a file

More information

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

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes.  Debugging tools Today Book-keeping File I/O Subscribe to sipb-iap-java-students Inner classes http://sipb.mit.edu/iap/java/ Debugging tools Problem set 1 questions? Problem set 2 released tomorrow 1 2 So far... Reading

More information

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

Software 1 with Java. Recitation No. 9 (Java IO) December 10, Software 1 with Java Recitation No. 9 (Java IO) December 10, 2006 1 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating files

More information

Mathematics/Science Department Kirkwood Community College. Course Syllabus. Computer Science CSC142 1/10

Mathematics/Science Department Kirkwood Community College. Course Syllabus. Computer Science CSC142 1/10 Mathematics/Science Department Kirkwood Community College Course Syllabus Computer Science CSC142 Bob Driggs Dean Cate Sheller Instructor 1/10 Computer Science (CSC142) Course Description Introduces computer

More information

Lecture 4. The Java Collections Framework

Lecture 4. The Java Collections Framework Lecture 4. The Java s Framework - 1 - Outline Introduction to the Java s Framework Iterators Interfaces, Classes and Classes of the Java s Framework - 2 - Learning Outcomes From this lecture you should

More information

Lecture 11.1 I/O Streams

Lecture 11.1 I/O Streams 21/04/2014 Ebtsam AbdelHakam 1 OBJECT ORIENTED PROGRAMMING Lecture 11.1 I/O Streams 21/04/2014 Ebtsam AbdelHakam 2 Outline I/O Basics Streams Reading characters and string 21/04/2014 Ebtsam AbdelHakam

More information

The Java Collections Framework. Chapters 7.5

The Java Collections Framework. Chapters 7.5 The Java s Framework Chapters 7.5 Outline Introduction to the Java s Framework Iterators Interfaces, Classes and Classes of the Java s Framework Outline Introduction to the Java s Framework Iterators Interfaces,

More information

Files and IO, Streams. JAVA Standard Edition

Files and IO, Streams. JAVA Standard Edition Files and IO, Streams JAVA Standard Edition Java - Files and I/O The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Files Two types: Text file and Binary file Text file (ASCII file) The file data contains only ASCII values

More information

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one Iterators What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one 9-2 2-2 What is an Iterator? An iterator is an abstract data

More information

C17: I/O Streams and File I/O

C17: I/O Streams and File I/O CISC 3120 C17: I/O Streams and File I/O Hui Chen Department of Computer & Information Science CUNY Brooklyn College 4/9/2018 CUNY Brooklyn College 1 Outline Recap and issues Review your progress Assignments:

More information

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2 Java IO - part 2 BIU OOP Table of contents 1 Basic Java IO What do we know so far? What s next? 2 Example Overview General structure 3 Stream Decorators Serialization What do we know so far? What s next?

More information

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List. Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 13 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete

More information

Introduction Unit 4: Input, output and exceptions

Introduction Unit 4: Input, output and exceptions Faculty of Computer Science Programming Language 2 Object oriented design using JAVA Dr. Ayman Ezzat Email: ayman@fcih.net Web: www.fcih.net/ayman Introduction Unit 4: Input, output and exceptions 1 1.

More information

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

Software 1 with Java. Recitation No. 7 (Java IO) May 29, Software 1 with Java Recitation No. 7 (Java IO) May 29, 2007 1 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating files Classes

More information

Compile error. 2. A run time error occurs when the program runs and is caused by a number of reasons, such as dividing by zero.

Compile error. 2. A run time error occurs when the program runs and is caused by a number of reasons, such as dividing by zero. (טיפול בשגיאות) Exception handling We learn that there are three categories of errors : Syntax error, Runtime error and Logic error. A Syntax error (compiler error) arises because a rule of the language

More information

C17: File I/O and Exception Handling

C17: File I/O and Exception Handling CISC 3120 C17: File I/O and Exception Handling Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/24/2017 CUNY Brooklyn College 1 Outline Recap and issues Exception Handling

More information

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept F1 A Java program Ch 1 in PPIJ Introduction to the course The computer and its workings The algorithm concept The structure of a Java program Classes and methods Variables Program statements Comments Naming

More information

CSE 143 Sp03 Midterm 2 Sample Solution Page 1 of 7. Question 1. (2 points) What is the difference between a stream and a file?

CSE 143 Sp03 Midterm 2 Sample Solution Page 1 of 7. Question 1. (2 points) What is the difference between a stream and a file? CSE 143 Sp03 Midterm 2 Sample Solution Page 1 of 7 Question 1. (2 points) What is the difference between a stream and a file? A stream is an abstraction representing the flow of data from one place to

More information

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4. 1 - What if the main method is declared as private? 1. The program does not compile 2. The program compiles but does not run 3. The program compiles and runs properly ( From Lectuer # 2) 4. The program

More information

Software 1. Java I/O

Software 1. Java I/O Software 1 Java I/O 1 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating files Classes for serializing objects 2 Streams A stream

More information

CN208 Introduction to Computer Programming

CN208 Introduction to Computer Programming CN208 Introduction to Computer Programming Lecture #11 Streams (Continued) Pimarn Apipattanamontre Email: pimarn@pimarn.com 1 The Object Class The Object class is the direct or indirect superclass of every

More information

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

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide Module 10 I/O Fundamentals Objectives Upon completion of this module, you should be able to: Write a program that uses command-line arguments and system properties Examine the Properties class Construct

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: www: materials: e-learning environment: office: alt. office: jkizito@cis.mak.ac.ug http://serval.ug/~jona http://serval.ug/~jona/materials/csc1214

More information

CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012

CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012 CMPSCI 187: Programming With Data Structures Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012 Files and a Case Study Volatile and Non-Volatile Storage Storing and Retrieving Objects

More information

Object-Oriented Programming Design. Topic : Streams and Files

Object-Oriented Programming Design. Topic : Streams and Files 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

More information

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List. Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 22 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete

More information

Chapter 17 Binary I/O. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Chapter 17 Binary I/O. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. Chapter 17 Binary I/O 1 Motivations Data stored in a text file is represented in human-readable form. Data stored in a binary file is represented in binary form. You cannot read binary files. They are

More information

Streams and File I/O

Streams and File I/O Chapter 9 Streams and File I/O Overview of Streams and File I/O Text File I/O Binary File I/O File Objects and File Names Chapter 9 Java: an Introduction to Computer Science & Programming - Walter Savitch

More information

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction OGIES 6/7 A- Core Java The Core Java segment deals with the basics of Java. It is designed keeping in mind the basics of Java Programming Language that will help new students to understand the Java language,

More information

Graduate-Credit Programming Project

Graduate-Credit Programming Project Graduate-Credit Programming Project Due by 11:59 p.m. on December 14 Overview For this project, you will: develop the data structures associated with Huffman encoding use these data structures and the

More information

File Input and Output Recitation 04/03/2009. CS 180 Department of Computer Science, Purdue University

File Input and Output Recitation 04/03/2009. CS 180 Department of Computer Science, Purdue University File Input and Output Recitation 04/03/2009 CS 180 Department of Computer Science, Purdue University Announcements Project 7 is out Fun project! With animation! Start early Exam grade has been released

More information

Contents Chapter 1 Introduction to Programming and the Java Language

Contents Chapter 1 Introduction to Programming and the Java Language Chapter 1 Introduction to Programming and the Java Language 1.1 Basic Computer Concepts 5 1.1.1 Hardware 5 1.1.2 Operating Systems 8 1.1.3 Application Software 9 1.1.4 Computer Networks and the Internet

More information

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC Implementing a List in Java CSC 143 Java List Implementation Using Arrays Updated with Java 5.0 Generics Reading: Ch. 13 Two implementation approaches are most commonly used for simple lists: Arrays Linked

More information

Objec&ves. Review. Standard Error Streams

Objec&ves. Review. Standard Error Streams Objec&ves Standard Error Streams Ø Byte Streams Ø Text Streams Oct 5, 2016 Sprenkle - CSCI209 1 Review What are benefits of excep&ons What principle of Java do files break if we re not careful? What class

More information

Performing input and output operations using a Byte Stream

Performing input and output operations using a Byte Stream Performing input and output operations using a Byte Stream public interface DataInput The DataInput interface provides for reading bytes from a binary stream and reconstructing from them data in any of

More information

Many Stream Classes. File I/O - Chapter 10. Writing Text Files. Text Files vs Binary Files. Reading Text Files. EOF with hasmoreelements()

Many Stream Classes. File I/O - Chapter 10. Writing Text Files. Text Files vs Binary Files. Reading Text Files. EOF with hasmoreelements() File I/O - Chapter 10 Many Stream Classes A Java is a sequence of bytes. An InputStream can read from a file the console (System.in) a network socket an array of bytes in memory a StringBuffer a pipe,

More information

List ADT. Announcements. The List interface. Implementing the List ADT

List ADT. Announcements. The List interface. Implementing the List ADT Announcements Tutoring schedule revised Today s topic: ArrayList implementation Reading: Section 7.2 Break around 11:45am List ADT A list is defined as a finite ordered sequence of data items known as

More information

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

Special error return Constructors do not have a return value What if method uses the full range of the return type? 23 Error Handling Exit program (System.exit()) usually a bad idea Output an error message does not help to recover from the error Special error return Constructors do not have a return value What if method

More information

Fundamental language mechanisms

Fundamental language mechanisms Java Fundamentals Fundamental language mechanisms The exception mechanism What are exceptions? Exceptions are exceptional events in the execution of a program Depending on how grave the event is, the program

More information