Lab 2: File Input and Output

Similar documents
Lab 4: Tracery Recursion in C with Linked Lists

Lecture 11.1 I/O Streams

1.00 Lecture 30. Sending information to a Java program

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

Lab 10: Sockets 12:00 PM, Apr 4, 2018

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

Simple Java Input/Output

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

Java Input/Output. 11 April 2013 OSU CSE 1

PIC 20A Streams and I/O

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

Programming Languages and Techniques (CIS120)

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

Sri Vidya College of Engineering & Technology Question Bank

Introduction Unit 4: Input, output and exceptions

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

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

Lecture 4: Exceptions. I/O

Software Practice 1 - File I/O

Internet Technology 2/7/2013

Programming Languages and Techniques (CIS120)

Inheritance E, xc Ex eptions ceptions I/O

Unit 10: exception handling and file I/O

Darshan Institute of Engineering & Technology for Diploma Studies

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

Objec&ves. Review. Standard Error Streams

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

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

CS 251 Intermediate Programming Java I/O Streams

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. Representation of Primitive Java Types. CSE143 Sp

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

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

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Exceptions. Examples of code which shows the syntax and all that

Errors and Exceptions

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

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

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

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Objec-ves JAR FILES. Jar files. Excep-ons. Files Streams. Ø Wrap up Ø Why Excep-ons? 9/30/16. Oct 3, 2016 Sprenkle - CSCI209 1

Exceptions - Example. Exceptions - Example

Exceptions. When do we need exception mechanism? When and why use exceptions? The purpose of exceptions

HST 952. Computing for Biomedical Scientists Lecture 8

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

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Pieter van den Hombergh Richard van den Ham. March 17, 2018

Input-Output and Exception Handling

Lecture 19 Programming Exceptions CSE11 Fall 2013

CS Programming I: File Input / Output

Shell Interface Assignment

Exceptions and Libraries

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

16-Dec-10. Consider the following method:

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

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

Programming Languages and Techniques (CIS120)

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

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

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

CS Programming I: File Input / Output

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

Object-Oriented Programming in the Java language

Exceptions. CSC207 Winter 2017

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

CSC 1214: Object-Oriented Programming

Getting Started in Java. Bill Pugh Dept. of Computer Science Univ. of Maryland, College Park

CS11 Java. Fall Lecture 4

Writing your own Exceptions. How to extend Exception

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS193j, Stanford Handout #26. Files and Streams

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

ITI Introduction to Computer Science II

CS 351 Design of Large Programs Sockets Example

Computer Science is...

Object-Oriented Programming Design. Topic : Streams and Files

OBJECT ORIENTED PROGRAMMING. Course 6 Loredana STANCIU Room B616

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

Exceptions and Working with Files

Check out FilesAndExceptions from SVN. Exam 2 Review File I/O, Exceptions Vector Graphics Project

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Unit 9: Network Programming

CS159. Nathan Sprague

CS September 2017

Checked and Unchecked Exceptions in Java

10/7/15. MediaItem tostring Method. Objec,ves. Using booleans in if statements. Review. Javadoc Guidelines

More on Objects in JAVA TM

BBM 102 Introduction to Programming II Spring Exceptions

Introduction to Software Engineering in Java. Exceptions, I/O, and you++

Techniques of Java Programming: Streams in Java

CSCI 200 Lab 2 Inheritance, Polymorphism & Data Streams

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Abstract Classes, Exceptions

Object Oriented Programming. Week 9 Part 1 File I/O

Outline. 15. Inheritance. Programming in Java. Computer Science Dept Va Tech August D Barnette, B Keller & P Schoenhoff

CSE 413 Winter 2001 Midterm Exam

Transcription:

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 the grammar from a file and writing the expanded grammar to the command line, we will also be able to read a grammar from the command line and write the expanded grammar to a file. To do this, you ll be learning about some of the IO (input/output) classes in Java, and how the Java concept of a Stream provides an abstraction layer above files and console I/O. You will be doing the following: Import the correct IO packages from the Standard Library Process command line arguments that determine whether the program is reading/writing from the console (command line) or from a file Load a grammar either from the console or from a file, based on the arguments the program was given Write the expanded grammar to either the command line or a file, based on the arguments the program was given Open the file TraceryRecursion.java that came with the lab. This is where you ll make all your changes. Every place in the file where you re supposed to make a change is marked with a comment labeled To Do. Importing packages in Java Try compiling the code we ve given you. If you haven t made any changes it ll tell you it can t find the following symbols: class Hashtable class PrintStream class InputStream But in Lab 1 we were able to use a Hashtable, so what s different? The problem is that we re trying to use something from the Java Standard Library without telling Java that we are. To tell Java that we re using something from the standard library, you use the import statement. In Lab 1 we had already included the correct imports for you. For lab 2 the compiler is telling you that you need to import these three classes in order to use them.

As an example of importing a class, to import Hashtable from the standard library so you can use it, you would put this statement at the beginning of your program: import java.util.hashtable The java.util part is the package (a collection of classes) that Hashtable belongs to. To figure out the package of a class you want to import from the standard library, you look at its documentation page. The documentation for Java s Hashtable is here. In addition to all the info about where it is in the type hierarchy and its accessible fields and methods, the documentation page says what package the class is part of at the top of the page right above the class name. To import PrintStream and InputStream you can look up the package they belong to at their respective documentation pages ( here and here ). Another trick to know about, though you don t really need to use it in this lab, is how to import a bunch of classes from the same package at once. To import all of the classes from a package, you can use statement: import <package name>.* Command line arguments This program can take the following command line arguments: -in <input file> -out <output file> The "-in" and "-out" part of the arguments allow us to specify the input and output file arguments in either order. Below are described the different valid combinations of arguments. $ java TraceryRecursion This reads the grammar from the console and writes the expanded grammar to the console. Reading from the console is called standard in and writing to the console is called standard out. Standard in is represented by the InputStream object stored in System.in, while standard out is represented by the PrintStream object stored in System.out. We will be reading from the console using pipes and the command cat (in

unix systems like Mac OS and Linux and gitbash for Windows). There is more about this below. $ java TraceryRecursion -in grammar-story.txt This reads a grammar from file grammar-story.txt and writes to standard out. This was the behavior you implemented in Lab 1. $ java TraceryRecursion -out grammar-out.txt This reads grammar from standard in and writes the expanded grammar to the text file grammar-out.txt. $ java TraceryRecursion -out grammar-out.txt -in grammar-story.txt This reads a grammar from from file grammar-story.txt and writes the expanded grammar to file grammar-out.txt. Note that since our command line arguments specify the input and output file with -in and -out you can put the arguments in either order (that is -out first like the example above, or -in first).your code doesn t need to handle bad command line arguments. Since -in and -out are what tell the program to use files, junk arguments will default the program to reading from standard in and writing to standard out. Processing command line arguments We use the command line arguments to figure out where we re reading the grammar from and where we re writing the grammar to. This is done with two helper functions that you need to write: public static InputStream getinputstream (String[] args) ; public static PrintStream getoutputstream (String[] args) ; There are comments marked with To Do in the file to remind you to define these methods. You ll be calling these methods in main, passing them the command line arguments, in order to determine the InputStream and PrintStream to use. The nice thing about these methods is that they abstract away whether you re reading/writing from standard IO or a file. The rest of the code can just read from the returned InputStream and write to the returned OutputStream without having to know or care whether its a file or the console.

To understand this, here s a bit more information about how Java abstracts input and output. You ll notice that getinputstream() returns an InputStream which is an abstract class (an abstract class, like an interface, can t be directly instantiated as an object). This means you will actually be returning a subclass of InputStream, where which subclass depends on whether it s the console or a file. If reading from a file, you ll be returning something called a FileInputStream. The diagram below shows where it sits in the class hierarchy for IO. A FileInputStream is the subclass of InputStream for reading from a File. A File is Java s representation of a file in the computer s file system. The field System.in stores an InputStream for standard input. To return the correct input location you ll: Look for a "-in" in args[] If there is an "-in" then you ll make a new FileInputStream that will take a new File named whatever is in args[] in the index after the location you found "-in". Return the FileInputStream. Otherwise return System.in which is also an InputStream You can assume that there will be 0, 2 or 4 command line arguments.

Now since we re dealing with user input and we don t know what exactly is in the folder with our program (from the program s point of view), we want to handle the possibility that the input file we were told to open doesn t exist. We ll want to use a try - catch block to catch a FileNotFoundException within getinputstream(). If you catch the exception, print the error message "Input grammar file does not exist." and return System.in (as if no -in argument was specified). Our function getoutputstream() is very similar to getinputstream() but instead of returning an InputStream we re returning a PrintStream (System.out is a PrintStream ). To return the correct output location you ll: Look for a "-out" in args[] If there is a "-out" then you ll make a new PrintStream that will take a new File named whatever is in args[] in the index after the location you found "-out". Return the PrintStream Otherwise return System.out which is also a PrintStream As above, you ll want to catch a FileNotFoundException, print the error message "Output file can not be created.", and return System.out (as if no -out argument was specified). Piping on the command line Another part of this lab is giving you practice using pipes on the command line to use the standard output of one program as the standard input of another. For our purposes we ll be using pipes to pipe the contents of a file into TraceryRecursion, but in general it s possible to hook lots of programs together on the command line using pipes. The general form of using pipes looks like <program 1 > <program 2 > where the character is the pipe. The line above would takes the standard output of <program 1 > (which would normally just print to the console) and feeds it into the standard input of <program 2 > (which would normally read interactively from the console).

To feed a file into the standard input of TraceryRecursion, we ll use the the unix command cat (short for concatenate). This command simply reads a file and writes the content to standard out. For example, if we type: cat grammar-story.txt this will print out the file contents to the console: animal:cat,emu,okapi Emotion:happy,sad,elated,curious,sleepy Color:red,green,blue Name:emily,luis,otavio,anna,charlie character:#name# the #adjective# #animal# place:school,the beach,the zoo,burning Man adjective:#color#,#emotion#, origin:once #character# and #character# went to #place# To take this output and feed it into TraceryRecursion we can pipe the output of cat into TraceryRecursion : cat grammar-story.txt java TraceryRecursion More generally this looks like: cat <file name> java TraceryRecursion <arguments> Try running TraceryRecursion without a -out argument using pipes to feed grammar files into TraceryRecursion. Loading the grammar For this lab, the loadgrammar() function will be very similar to what it was in Lab 1 with one major difference: instead of giving it a string as an argument we re passing it the InputStream we returned from getinputstream().

We re going to use an instance of BufferedReader t o read from this InputStream. The main reason for using this class instead of the raw InputStream is for the function readline() which allows us to read strings that correspond to a line of the file, automatically handling the fact that different operating systems using different characters to indicate the end of a line. You can read more about BufferedReaders here. To make a new BufferedReader you need to give it a new InputStreamReader which requires an InputStream as part of its constructor. You can see the details of an InputStreamReader here. So you ll be creating a new BufferedReader from a new InputStreamReader which is created from the InputStream. Note: The constructor for BufferedReader wants an instance of the abstract class Reader as the argument. An InputStreamReader is a subclass of Reader, which is why this works. We ll use the readline() method on BufferedReader to read each line of the grammar and add it to the Hashtable. The loop where you re calling readline() will have to know when to stop. Fortunately, it returns null when it gets to the end of the stream, so we can use this to know when the loop should stop. Specifying that loadgrammar() throws an exception You ve probably seen at least a few RuntimeExceptions being thrown in your experience as a Java programmer. Among the most common are the ArrayOutOfBoundsException (when you re trying to access an index that isn t in an array) and a NullPointerException (when you try to access a field or method on a reference variable that hasn t been set equal to anything). These are unchecked exceptions, and programmers don t usually try to catch them. The BufferedReader methods in loadgrammar() throw the checked exception IOException. Since it s checked, the compiler flags it as an error, requiring the programmer to either catch the exception or declare that the method can throw the exception. For example, to declare that main() can throw an IOException, you d change the signature to: public static void main (String[] args) throws IOException You ll need to do this for loadgrammar(). When you declare that a method throws a checked exception, the compiler is happy, because now it can check that anyone who

calls the method that can throw an exception either catches the exception or declares that they throw the exception in turn. Changes in main() There are two things you ll need to change in main() : Calling the methods getinputstream() and getprintstream() to get the InputStream and PrintStream based on the arguments Changing the println() in main() to be called on the PrintStream that getoutputstream() returned Outputting the grammar Unlike in Lab 1, the method outputgrammar() has two arguments. Like the previous lab, the first one is the grammar we re expanding. The second is the PrintStream we re going to print the grammar to. All we have to do here is to call println() on the PrintStream argument rather than on standard output. Example arguments and output $ java TraceryRecursion -in grammar-story.txt -out output.txt Running TraceryRecursion... Set seed 1 The contents of output.txt are: GRAMMAR: adjective: place: emotion: origin: #place#", color: name: character: "#color#", "#emotion#", "school", "the beach", "the zoo", "Burning Man", "happy", "sad", "elated", "curious", "sleepy", "once #character# and #character# went to "red", "green", "blue", "emily", "luis", "otavio", "anna", "charlie", "#name# the #adjective# #animal#",

animal: "cat", "emu", "okapi", once otavio the blue emu and luis the curious emu went to school once otavio the elated cat and charlie the blue emu went to school once anna the elated okapi and charlie the sad okapi went to Burning Man once emily the happy okapi and otavio the red cat went to school once otavio the blue cat and emily the sleepy okapi went to the beach once luis the green cat and charlie the blue emu went to school once otavio the red emu and otavio the red okapi went to the zoo once charlie the green cat and luis the green okapi went to the zoo once luis the red emu and emily the red cat went to school once otavio the sad okapi and luis the happy emu went to school Turning the code in Create a directory with the following name: <student ID>_lab2 where you replace <student ID> with your actual student ID. For example, if your student ID is 1234567, then the directory name is 1234567 _lab2. Put a copy of your edited TraceryRecursion.java file in the directory. Compress the folder using zip. Zip is a compression utility available on mac, linux and windows that can compress a directory into a single file. This should result in a file named <student ID>_lab2.zip (with <student ID> replaced with your real ID of course). Upload the zip file through the page for Lab 2 in canvas ( https://canvas.ucsc.edu/courses/12730/assignments/39627 ). Helpful tips: Packages: Overview of using package members: https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html Try-Catch: Overview of catch blocks in Java: https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html Throwables in Java: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

I/O Streams Relationship of the various Input and Output Streams: https://stackoverflow.com/questions/22563986/understanding-getinputstre am-and-getoutputstream