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

Size: px
Start display at page:

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

Transcription

1 CISC 323 (Week 9) Design of a Weather Program & Java File I/O Jeremy Bradbury Teaching Assistant March 8 & 10, 2004 bradbury@cs.queensu.ca Programming Project The next three assignments form a programming project. Assignment #3: Object-oriented analysis Assignment #4: Detailed project design Assignment #5: Project implementation Assignment #4 Assignment #4 Due Date: Monday, March 15, 2003 (1:00 pm) Move from a statement of the requirements to a design of how your program will be structured to accomplish these goals. Use layered approach business, access, and view (each class should belong to exactly one layer!) Task 1: Big Class Diagram of program [only class names] (6 marks) Task 2: Detailed Class Diagram for each class in Task 1 (6 marks) Task 3: GUI Design [rough pictures of frames and dialogs] (3 marks) Task 4: Sequence diagram [based on use case diagram] (3 marks) Task 5: Activity diagram [for a method with non-trivial loop] (2 marks) TOTAL = 20 marks

2 Java File I/O Streams A stream is an object that transfers data. Disclaimer: The following overview of Java File I/O and exceptions are slides from CISC 124 (Fall 2003) written by M. Lamb. Input stream: transfers data from a source to the program Output stream: transfers data from the program to a destination CISC 124, fall 2003, I/O 4 Exceptions I/O can throw lots of exceptions: device error file doesn't exist file is protected end of file (in some cases) IOException FileNotFoundException EOFException These are not subclasses of RuntimeException. Compiler will complain if you don't handle them. Difficult to test some error conditions (device errors) Still should write code to handle them Output Streams An OutputStream is an object that knows how to send bytes to a destination A FileOutputStream is an object that knows how to send bytes to a file bytes can be any binary values method in OutputStream: void write(int b) throws IOException creating a FileOutputStream: new FileOutputStream(String name) new FileOutputStream(String name, boolean append) Both can throw FileNotFoundException CISC 124, fall 2003, I/O 5 CISC 124, fall 2003, I/O 6

3 File Names In Java, a file name is a String that would be a valid file name on your operating system. myfile.txt c:\users\margaret\assignment3.java Warning: backslash is the escape character in Java String filename = "c:\cisc124\data.txt"; Gets compiler error. Two ways around this: 1. Double backslash means a real backslash in the string String filename = "c:\\cisc124\\data.txt"; 2. Java accepts forward slash instead String filename = "c:/cisc124/data.txt"; CISC 124, fall 2003, I/O 7 Write a short message to a file: Simple Example OutputStream outstream = null; // keep compiler happy outstream = new FileOutputStream("OutputTest.txt"); System.out.println("Error in opening output file"); // end try/catch // write to the output stream // write individual bytes outstream.write('h'); outstream.write('e'); outstream.write('l'); outstream.write('l'); outstream.write('o'); System.out.println("Error in writing to output file"); // end try/catch CISC 124, fall 2003, I/O 8 Output Buffering If we run the preceeding program, the file may not contain all the characters we wrote common student debugging problem Reason: output buffering Program data Buffer in memory data File on disk Finishing the Previous Example outstream.close(); System.out.println("error in closing output file"); // end try/catch Now file OutputTest.txt will exist and contain the line: hello When we're done writing to file, we must close the file. This flushes the buffer and frees up system resources OutputStream method: void close() throws IOException CISC 124, fall 2003, I/O 9 CISC 124, fall 2003, I/O 10

4 Folders Previous example created file with name "OutputTest.txt" Where does this file go (what folder)? Answer: same folder as your program (Java files) Good enough for assignments If you want to create a file in a different folder, must put folder into file name: "subfolder/outputtest.txt" "../otherfolder/outputtest.txt" "c:/myfolder/outputtext.txt" Text Files A FileOutputStream writes individual bytes. Very flexible: bytes can be any values at all not great for writing text files Text File: contains ASCII (or Unicode) characters. Conceptually separated into lines by newline characters ('\n') Examples:.txt,.java,.bat,.html Operating systems have conventions about text files Binary File: contains data coded in binary Examples:.class,.exe,.pdf,.doc (will discuss later) CISC 124, fall 2003, I/O 11 CISC 124, fall 2003, I/O 12 Writer a Writer writes characters to a text file Creating a PrintWriter (1) A PrintWriter object uses an OutputStream Formats the output figures out what characters to write and sends the characters to the OutputStream value: "hello" class PrintWriter: writes formatted text output to a stream methods will look familiar: print println Overloaded to take any primitive type or objects Also a close method. None of these methods throw exceptions. PrintWriter OutputStream sequence of bytes: 'h','e','l','l','o',end of line CISC 124, fall 2003, I/O 13 CISC 124, fall 2003, I/O 14

5 Creating a PrintWriter (2) To create a PrintWriter, first create an OutputStream OutputStream outstream = null; // keep compiler happy // create the output stream outstream = new FileOutputStream(fileName); System.out.println("Error in opening output file"); // end try/catch Creating a PrintWriter (3) More compressed version: PrintWriter writer = null; writer = new PrintWriter( new FileOutputStream("outputfile.txt")); System.out.println("Error in opening output file"); // create PrintWriter to write formatted output PrintWriter writer = new PrintWriter(outStream); CISC 124, fall 2003, I/O 15 CISC 124, fall 2003, I/O 16 Using the PrintWriter // write a string writer.println("hello, world!"); // write some other types writer.print("the year is "); writer.print(2001); writer.print(", the current temperature is "); writer.println(15.4); writer.print("and my boolean variable is "); boolean b = true; writer.println(b); writer.close() The Standard Output Writing to the "standard output" (the computer screen): System.out.print System.out.println System is a class in the API. System.out is a static field in System type of System.out is PrintStream PrintStream is similar to PrintWriter The file will contain: hello, world! the year is 2001, the current temperature is 15.4 and my boolean variable is true CISC 124, fall 2003, I/O 17 CISC 124, fall 2003, I/O 18

6 The File Class a File object contains a file name many useful methods: boolean canread() boolean canwrite() boolean delete() boolean exists() long length() constructors: File(String pathname) File(String parent, String child) File(File parent, String child) Can use instead of a file name to create OutputStreams & other I/O objects CISC 124, fall 2003, I/O 19 Example File destination = new File("outputfile.txt"); if (destination.exists()) { System.out.println("Output file exists."); System.out.print( "Do you want to overwrite it (y/n)? "); String answer = Stdin.readString(); if (!answer.equalsignorecase("y")) { System.out.println("aborting"); // end if // end if... outstream = new FileOutputStream(destination); CISC 124, fall 2003, I/O 20 Text Input The preceding slides told you how to write to a text file. Now we need to know how to read from a text file. Using FileReaders Reader has method: int read() throws IOException Reads one character Returns -1 if at end of file Usually don't use FileReader directly a FileReader reads single characters from a text file. Create using a file name or File: FileReader(String filename) throws FileNotFoundException FileReader(File file) throws FileNotFoundException CISC 124, fall 2003, I/O 21 CISC 124, fall 2003, I/O 22

7 BufferedReader A BufferedReader can read a line at a time -- more convenient Also more efficient because input is buffered. Create a BufferedReader from a simpler reader: BufferedReader(Reader in) Has the read method from Reader, plus an additional method: String readline() throws IOException String returned by readline does not include an end-of-line character When at end of file, readline returns null. Important note: null is different from ""! CISC 124, fall 2003, I/O 23 BufferedReader Example String filename = "sample.txt"; BufferedReader reader = new BufferedReader( new FileReader(filename)); String inputline; // a line of input from the file while (true) { // exit with break inputline = reader.readline(); if (inputline == null) break; System.out.println(inputLine); // end while reader.close(); catch (FileNotFoundException e) { System.out.println("Error: " + filename + " not found"); System.out.println("I/O error while reading"); CISC 124, fall 2003, I/O 24 What About Reading Other Types? BufferedReader can read chars or Strings What about int, double, boolean, etc? There are no methods to read these directly. Instead, read a String and translate into another type String to int: int i = Integer.parseInt(s); String to double: double d = Double.parseDouble(s); These can throw NumberFormatException Example (1) Suppose we expect our input file to have the following contents: int double boolean (each on separate line) Parse methods will throw NumberFormatException if the int & double are not formatted correctly We will use NumberFormatException for other errors as well Important to know what format to expect in file. CISC 124, fall 2003, I/O 25 CISC 124, fall 2003, I/O 26

8 Example (2) BufferedReader reader = new BufferedReader(...); // read the int String inputline = reader.readline(); if (inputline == null) throw new NumberFormatException(); int theint = Integer.parseInt(inputLine); System.out.println("the integer is: " + theint); Example (3) // read the double inputline = reader.readline(); if (inputline == null) throw new NumberFormatException(); double thedouble = Double.parseDouble(inputLine); System.out.println("the double is: " + thedouble); CISC 124, fall 2003, I/O 27 CISC 124, fall 2003, I/O 28 Example (4) // read the boolean inputline = reader.readline(); if (inputline == null) throw new NumberFormatException(); boolean theboolean; if (inputline.equalsignorecase("true")) theboolean = true; else if (inputline.equalsignorecase("false")) theboolean = false; else throw new NumberFormatException(); System.out.println("the boolean is: " + theboolean); Example, finished catch (NumberFormatException e) { System.out.println("illegal file format"); catch (FileNotFoundException e) { System.out.println("Error: file " + filename + " not found"); System.out.println("I/O error while " + "reading from file"); reader.close(); CISC 124, fall 2003, I/O 29 CISC 124, fall 2003, I/O 30

9 Java File I/O Resources CISC 323 Website mples/io.html mples/schedulewithexceptions.zip mples/testfiledemo.java Java Website index.html

Simple Java Input/Output

Simple Java Input/Output Simple Java Input/Output Prologue They say you can hold seven plus or minus two pieces of information in your mind. I can t remember how to open files in Java. I ve written chapters on it. I ve done it

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

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

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

Java Input / Output. CSE 413, Autumn 2002 Programming Languages. Java Input / Output CSE 413, Autumn 2002 Programming Languages http://www.cs.washington.edu/education/courses/413/02au/ 18-November-2002 cse413-18-javaio 2002 University of Washington 1 Reading Readings

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

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

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

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

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

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

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

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

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

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

Byte and Character Streams. Reading and Writing Console input and output Byte and Character Streams Reading and Writing Console input and output 1 I/O basics The io package supports Java s basic I/O (input/output) Java does provide strong, flexible support for I/O as it relates

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

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

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

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 30 April 4, 2018 I/O & Histogram Demo Chapters 28 HW7: Chat Server Announcements No penalty for late submission by tomorrow (which is a HARD deadline!)

More information

Unit 10: exception handling and file I/O

Unit 10: exception handling and file I/O Unit 10: exception handling and file I/O Using File objects Reading from files using Scanner Writing to file using PrintStream not in notes 1 Review What is a stream? What is the difference between a text

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

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

CS 200 File Input and Output Jim Williams, PhD

CS 200 File Input and Output Jim Williams, PhD CS 200 File Input and Output Jim Williams, PhD This Week 1. WaTor Change Log 2. Monday Appts - may be interrupted. 3. Optional Lab: Create a Personal Webpage a. demonstrate to TA for same credit as other

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

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

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

CS Week 11. Jim Williams, PhD

CS Week 11. Jim Williams, PhD CS 200 - Week 11 Jim Williams, PhD This Week 1. Exam 2 - Thursday 2. Team Lab: Exceptions, Paths, Command Line 3. Review: Muddiest Point 4. Lecture: File Input and Output Objectives 1. Describe a text

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

Variables of class Type. Week 8. Variables of class Type, Cont. A simple class:

Variables of class Type. Week 8. Variables of class Type, Cont. A simple class: Week 8 Variables of class Type - Correction! Libraries/Packages String Class, reviewed Screen Input/Output, reviewed File Input/Output Coding Style Guidelines A simple class: Variables of class Type public

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

When we reach the line "z = x / y" the program crashes with the message:

When we reach the line z = x / y the program crashes with the message: CSCE A201 Introduction to Exceptions and File I/O An exception is an abnormal condition that occurs during the execution of a program. For example, divisions by zero, accessing an invalid array index,

More information

CS11 Java. Fall Lecture 4

CS11 Java. Fall Lecture 4 CS11 Java Fall 2014-2015 Lecture 4 Java File Objects! Java represents files with java.io.file class " Can represent either absolute or relative paths! Absolute paths start at the root directory of the

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

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

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

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

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

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

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

Input from Files. Buffered Reader

Input from Files. Buffered Reader Input from Files Buffered Reader Input from files is always text. You can convert it to ints using Integer.parseInt() We use BufferedReaders to minimize the number of reads to the file. The Buffer reads

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

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

Input-Output and Exception Handling

Input-Output and Exception Handling Software and Programming I Input-Output and Exception Handling Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline Reading and writing text files Exceptions The try block catch and finally

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

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

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

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

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

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

Sri Vidya College of Engineering & Technology Question Bank

Sri Vidya College of Engineering & Technology Question Bank 1. What is exception? UNIT III EXCEPTION HANDLING AND I/O Part A Question Bank An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program s instructions.

More information

Java Input/Output. 11 April 2013 OSU CSE 1

Java Input/Output. 11 April 2013 OSU CSE 1 Java Input/Output 11 April 2013 OSU CSE 1 Overview The Java I/O (Input/Output) package java.io contains a group of interfaces and classes similar to the OSU CSE components SimpleReader and SimpleWriter

More information

IT101. File Input and Output

IT101. File Input and Output IT101 File Input and Output IO Streams A stream is a communication channel that a program has with the outside world. It is used to transfer data items in succession. An Input/Output (I/O) Stream represents

More information

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015 Announcements Assignment 4 is posted and Due on 29 th of June at 11:30 pm. Course Evaluations due

More information

Streams and File I/O

Streams and File I/O 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 in a file Read data in a file The Concept of a Stream Use

More information

File I/O Introduction to File I/O Text Files The File Class Binary Files 614

File I/O Introduction to File I/O Text Files The File Class Binary Files 614 10.1 Introduction to File I/O 574 Streams 575 Text Files and Binary Files 575 10.2 Text Files 576 Writing to a Text File 576 Appending to a Text File 583 Reading from a Text File 586 Reading a Text File

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

STREAMS. (fluxos) Objetivos

STREAMS. (fluxos) Objetivos STREAMS (fluxos) Objetivos To be able to read and write files To become familiar with the concepts of text and binary files To be able to read and write objects using serialization To be able to process

More information

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

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction COMPSCI 230 S2 2016 Software Construction File Input/Output 2 Agenda & Reading Agenda: Introduction Byte Streams FileInputStream & FileOutputStream BufferedInputStream & BufferedOutputStream Character

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 30 November 13, 2015 ExcepEons / IO Chapter 27 HW7: PennPals Chat Due: Tuesday, November 17 th Announcements Start today if you haven't already! Poll

More information

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

Here is a hierarchy of classes to deal with Input and Output streams. PART 25 25. Files and I/O 25.1 Reading and Writing Files A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data

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 in a file Read data from

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

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions Introductory Programming Exceptions and I/O: sections 80 83 Anne Haxthausen a IMM, DTU 1 Exceptions (section 80) 2 Input and output (I/O) (sections 81-83) a Parts of this material are inspired by/originate

More information

Introductory Programming Exceptions and I/O: sections

Introductory Programming Exceptions and I/O: sections Introductory Programming Exceptions and I/O: sections 80 83 Anne Haxthausen a IMM, DTU 1 Exceptions (section 80) 2 Input and output (I/O) (sections 81-83) a Parts of this material are inspired by/originate

More information

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

Here is a hierarchy of classes to deal with Input and Output streams. PART 15 15. Files and I/O 15.1 Reading and Writing Files A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data

More information

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

CSB541 Network Programming 網路程式設計. Ch.2 Streams 吳俊興國立高雄大學資訊工程學系 CSB541 Network Programming 網路程式設計 Ch.2 Streams 吳俊興國立高雄大學資訊工程學系 Outline 2.1 Output Streams 2.2 Input Streams 2.3 Filter Streams 2.4 Readers and Writers 2 Java I/O Built on streams I/O in Java is organized

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 31 November 17, 2017 I/O & Histogram Demo Chapters 28 Announcements HW8: SpellChecker Available on the website and Codio Due next Tuesday, November

More information

Lab 2: File Input and Output

Lab 2: File Input and Output Lab 2: File Input and Output This lab introduces how to handle files as both input and output. We re coming back to Tracery (which you implemented in Lab 1) with this assignment but instead of always reading

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

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015 COMP-202: Foundations of Programming Lecture 22: File I/O Jackie Cheung, Winter 2015 Announcements Assignment 5 due Tue Mar 31 at 11:59pm Quiz 6 due Tue Apr 7 at 11:59pm 2 Review 1. What is a graph? How

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 31 April 6 th, 2016 I/O Chapter 28 Poll Did you finish HW 07 PennPals? 1. Yes! 2. I turned it in on time, but there are a few things I couldn't figure

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

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

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

CS Programming I: File Input / Output

CS Programming I: File Input / Output CS 200 - Programming I: File Input / Output Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624

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

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

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

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output Overview 7 Streams and files import java.io.*; Binary data vs textual data Simple file processing - examples The stream model Bytes and characters Buffering Byte streams Character streams Binary streams

More information

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University Lecture 2 COMP1406/1006 (the Java course) Fall 2013 M. Jason Hinek Carleton University today s agenda a quick look back (last Thursday) assignment 0 is posted and is due this Friday at 2pm Java compiling

More information

When working with files and directories, it is often

When working with files and directories, it is often VERIFY THAT A FILE OR DIRECTORY EXISTS When working with files and directories, it is often necessary to verify that a file or directory exists before performing an action. For example, you should verify

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

10/8/2018 Programming Data Structures. class diagram for assignment 08 review: polymorphism review: exception new: File I/O

10/8/2018 Programming Data Structures. class diagram for assignment 08 review: polymorphism review: exception new: File I/O 10/8/2018 Programming Data Structures class diagram for assignment 08 review: polymorphism review: exception new: File I/O 1 class diagram (informal) link the parent classes and child classes describe

More information

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014 CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014 Name: This exam consists of 5 problems on the following 6 pages. You may use your double- sided hand- written 8 ½ x 11 note sheet

More information

Software Practice 1 - File I/O

Software Practice 1 - File I/O Software Practice 1 - File I/O Stream I/O Buffered I/O File I/O with exceptions CSV format Practice#6 Prof. Joonwon Lee T.A. Jaehyun Song Jongseok Kim (42) T.A. Sujin Oh Junseong Lee 1 (43) / 38 2 / 38

More information

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

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure. Java in 21 minutes hello world basic data types classes & objects program structure constructors garbage collection I/O exceptions Strings Hello world import java.io.*; public class hello { public static

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

Text User Interfaces. Keyboard IO plus

Text User Interfaces. Keyboard IO plus Text User Interfaces Keyboard IO plus User Interface and Model Model: objects that solve problem at hand. User interface: interacts with user getting input from user giving output to user reporting on

More information

Simple Java I/O. Part I General Principles

Simple Java I/O. Part I General Principles Simple Java I/O Part I General Principles Streams All modern I/O is stream based A stream is a connec8on to a source of data or to a des8na8on for data (some8mes both) An input stream may be associated

More information

I/O STREAM (REQUIRED IN THE FINAL)

I/O STREAM (REQUIRED IN THE FINAL) I/O STREAM (REQUIRED IN THE FINAL) STREAM A stream is a communication channel that a program has with the outside world. It is used to transfer data items in succession. An Input/Output (I/O) Stream represents

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

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Fundamental Java Syntax Summary Sheet for CISC101, Spring Java is Case - Sensitive!

Fundamental Java Syntax Summary Sheet for CISC101, Spring Java is Case - Sensitive! Fundamental Java Syntax Summary Sheet for CISC101, Spring 2006 Notes: Items in italics are things that you must supply, either a variable name, literal value, or expression. Variable Naming Rules Java

More information

23 Error Handling What happens when a method is called? 23.1 What is Exception Handling? A.m() B.n() C.p()

23 Error Handling What happens when a method is called? 23.1 What is Exception Handling? A.m() B.n() C.p() 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

CS193j, Stanford Handout #26. Files and Streams

CS193j, Stanford Handout #26. Files and Streams CS193j, Stanford Handout #26 Summer, 2003 Manu Kumar Files and Streams File The File class represents a file or directory in the file system. It provides platform independent ways to test file attributes,

More information

Computer Science is...

Computer Science is... Computer Science is... Computational complexity theory Complexity theory is the study of how algorithms perform with an increase in input size. All problems (like is n a prime number? ) fit inside a hierarchy

More information

Fall 2017 CISC124 10/1/2017

Fall 2017 CISC124 10/1/2017 CISC124 Today First onq quiz this week write in lab. More details in last Wednesday s lecture. Repeated: The quiz availability times will change to match each lab as the week progresses. Useful Java classes:

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