OOP&M - theory lectures1 OOP&M – the second day “… In the beginning God created the heavens and the earth …” - the Hollybook -

Slides:



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

1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
OOP&M - theory lectures1 OOP&M – the third day “… I cannot help with this terrible headache …” - Bjorn the morning after -
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
1 File Output. 2 So far… So far, all of our output has been to System.out  using print(), println(), or printf() All input has been from System.in 
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input.
Files and Streams CS 21a. 10/02/05 L18: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
OOP&M - laboratory lectures1 OOP&M – LAB2 LABzwei: the Input.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
Chapter 3b Standard Input and Output Sample Development.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
OOP&M - laboratory lectures1 JPN – GUI and Applets “… I am sure ‘it wont fail as much as the one before …” - Bill Gates about W98 -
OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation.
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
1 Course Lectures Available on line:
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Input / Output Chapter 13.  We use files all the time  Programs are files  Documents are files  We want them to be “permanent”  To last beyond execution.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
I/O in Java Dennis Burford
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Two Ways to Store Data in a File  Text format  Binary format.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
CSE 1020:Aggregation Mark Shtern 1-1. Summary Development process Testing Debugging 1-2.
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Intro to Computer Science Class #8 Exceptions and I/O Instructor: Ms. Catherine Stocker Teaching Assistants: Alex, Katie, Siraaj, Isaiah, Allison, Thibault.
java.io supports console and file I/O
CHAPTER 3 File Output.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Streams & File Input/Output (I/O)
Program Input/Output (I/O)
Interactive Standard Input/output
12: The Java I/O System stream model.
I/O Basics.
I/O Streams- Basics Byte Streams and Character Streams
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Streams and File I/O Chapter 14.
JAVA IO.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
File Input and Output.
LCC 6310 Computation as an Expressive Medium
EEC 484/584 Computer Networks
Presentation transcript:

OOP&M - theory lectures1 OOP&M – the second day “… In the beginning God created the heavens and the earth …” - the Hollybook -

OOP&M - theory lectures2 OOP&M – do you remember? inputoutput -joystick -mouse -keyboard -microph. human based input -floppy -HD -CD -ZIP/JAZZ -… prev. rec. infor. as input -joystick -screen -speakers -lights human based output -floppy -HD -CD -ZIP/JAZZ -… infor. as output

OOP&M - theory lectures3 monitor  object System.out is the predefined reference to the monitor in the PrintStream class displayread programmonitor Bjorn System.out.println(“ In winter Bjorn and Lisa eat potatoes.”) println(“ In winter Bjorn and Lisa eat potatoes.”) behaviordetails reference to the receiver message OOP&M – do you remember?

OOP&M - theory lectures4 Floppies, HardDrives, ZIP disks, CDs … contain information in the form of files. Attributes of files: contents file name Java provides a predefined class for modeling disk files, called File file  object Java program FileOutputStream SHOPPING potatis mjolk agg … shopping.lst bytes of data ewsc24rfds53Hej20Hur20m76ar20du20?rsf OOP&M – do you remember?

OOP&M - theory lectures5 import java.io.* class LogMyDay { public static void main(String[] arg) throws Exception{ PrintStream log; FileOutputStream logfileStream; File logfile; logfile = new File(“Bjorn_Lisa.log”); logfileStream = new FileOutputStream(logfile); log = new PrintStream(logfileStream); System.out.println(“16:30 Met at Triangeln …”); log.println(“16:30 Met at Triangeln …”); … } one LogMyDay example OOP&M – do you remember?

OOP&M - theory lectures6 OOP&M – the second day “… And God saw that the light was good; and God separated the light from the darkness …” - the Hollybook -

OOP&M - theory lectures7 OOP&M – input / output to programs Java program keyboard monitor files network ? ?? System.out FileOutputStream Java program ? ?

OOP&M - theory lectures8 OOP&M – input … an overview We have not yet talked about how to use information coming from a keyboard or stored in a file … THE INPUT This forces us to place all the information into the programs themselves Because of this our programs are: inefficient non reusable …

OOP&M - theory lectures9 OOP&M – input … an overview Example: we want to delete a file, then we need a code that would look like the following: Filef; f = new File(“delete_me”); f.delete(); delete But with this we can only delete a file called: “delete_me” Wouldn’t it be much better if the program could have as an input the name of the file to be deleted? So we could call it with a line like: “ delete filename ”

OOP&M - theory lectures10 OOP&M – input … an overview Java program InputStream bytes of data ewsc24rfds53Hej20Hur20m76ar20du20?rsf Input source We must build a bridge between the Java Program and the input source!!

OOP&M - theory lectures11 While describing the output we saw that write data to a file involved several stages: Create a File object Create a FileOutputStream object Create a PrintStream object OOP&M – input … an overview Input – whether coming from the keyboard, from a file, from the network or from another program – requires a similar sequence of stages

OOP&M - theory lectures12 Stages OOP&M – input … an overview Construct a reference to an InputStream object, and with it Construct an InputStreamReader object, and with it Construct a BufferedReader object This references could be of two types FileInputStreamReader for files BufferedInputStream for keyboard and the network Note that: InputStream inputs sequences of bytes into the program InputStreamReader models the stream of input as characters, but doesn’t recognize ends of line a.o. BufferedReader has got a collection of methods that allow us to work in a similar way as we did with PrintStream

OOP&M - theory lectures13 OOP&M – a board with keys Java provides a predefined object to represent the stream of input that comes from the keyboard. System.in is a reference to this object contained into the BufferedInputStream class Unlike System.out, which refers to a PrintStream object and therefore can be used right away to write Strings to the monitor, System.in, a reference to a BufferedInputStream object, cannot be readily used to read Strings keyboard [existing] InputStream new InputStreamReader new BufferedReader keyb isr System.in

OOP&M - theory lectures14 OOP&M – a board with keys the constructor for InputStreamReader accepts the keyboard (a BufferedInputStream reference) as its argument new InputStreamReader ( System.in ) Java provides System.in contained into the BufferedInputStream class the constructor for BufferedReader accepts a InputStreamReader reference as its argument new BufferedReader ( ISR )

OOP&M - theory lectures15 OOP&M – a board with keys The total declaration would be: InputStreamReader isr; BufferedReader keyb; isr = new InputStreamReader(System.in); keyb = new BufferedReader(isr); And now, for reading a line: InputStreamReader isr; BufferedReader keyb; String inputline; isr = new InputStreamReader(System.in); keyb = new BufferedReader(isr); inputline = keyb.readLine(); *.readLine() is a method from the BufferedReader class

OOP&M - theory lectures16 OOP&M – a board with keys Example: a program that writes the “plural” of a word import java.io.*; /* * This program writes the plural of the word typed in the * keyboard … it just adds an “s” */ class plural { public static void main(String[] arg) throws Exception{ InputStreamReader isr; BufferedReader keyb; String inputline; isr = new InputStreamReader(System.in); keyb = new BufferedReader(isr); inputline = keyb.readLine(); System.out.print(inputline); System.out.println(“s”); }

OOP&M - theory lectures17 OOP&M – a problem: interactivity The keyboard involves directly to a human being, often termed an ENDUSER ENDUSERs are almost never the authors of the programs that they use ENDUSERs cannot be expected to automatically know what to type on the keyboard and when to type it A program that expects input from a keyboard must provide some information to the users as it runs It must display messages such as: “If you want me to delete all your hard-drive, press [Y]es, in other case, pray something!”

OOP&M - theory lectures18 OOP&M – a problem: interactivity This kind of messages that ask the user to do something are called: “prompts” In order to improve the interactivity of our “plurals” program, we should add a prompt like: … isr = new InputStreamReader(System.in); keyb = new BufferedReader(isr); System.out.println(“Type in a word to pluralize ”); inputline = keyb.readLine(); System.out.print(inputline); System.out.println(“s”); …

OOP&M - theory lectures19 OOP&M – a problem: interactivity But … did you really think that it was going to be so easy?!? OF COURSE, IF OUR AIM WAS TO IMPROVE INTERACTIVITY, THIS IS NOT GOING TO WORK – Murphy’s principle - The reason resides in the behavior for the System.out as it is defined in the PrintStream class A PrintStream object receives print and println messages and will EVENTUALLY display the Strings requested

OOP&M - theory lectures20 OOP&M – a problem: interactivity But … how does PrintStream work?!? Imagine Bjorn having to cook a dinner for friends. He wants to prepare some backed potatoes. Does it have a sense to peel one potato and back it and then continue with other one? Bjorn will cook as many potatoes as possible at once, because in other case he would be wasting his time!! Bjorn oven potatoes

OOP&M - theory lectures21 OOP&M – a problem: interactivity But … how does PrintStream work?!? With the computer happens exactly the same problem. There is a part of the memory that is allocated for streaming operations. It is called a buffer. There the computer stores as many information as it cans, and shows it at once, when it is full. But, there is a method that forces the strings to be shown on the screen. *.flush() forces the computer to empty the buffer. buffer string1 string2 string3

OOP&M - theory lectures22 OOP&M – a problem: interactivity The use of this method will allow us the improvement of interactivity The buffer shall be “flushed” in order to show the prompt to the user: … isr = new InputStreamReader(System.in); keyb = new BufferedReader(isr); System.out.println(“Type in a word to pluralize ”); System.out.flush(); inputline = keyb.readLine(); System.out.print(inputline); System.out.println(“s”); …

OOP&M - theory lectures23 OOP&M – come IN PUT files Obtaining input from disk files is only a little bit more complicated than from the keyboard. Our starting point must be to find some sort of InputStream object. As we said before, both BufferedInputStream and FileInputStream belong to that class new FileInputStream new InputStreamReader new BufferedReader bsr isr System.in is already defined into the class BufferedInputStream, the problem is that the FileInputStream class does not have defined objects like that. Therefore we need to first define an object that makes reference to a file. fsr files

OOP&M - theory lectures24 OOP&M – come IN PUT files the constructor for InputStreamReader accepts a stream from a file (a FileInputStream reference) as its argument new InputStreamReader ( FileInputStream ) the constructor for FileInputStream accepts a file (a File reference) as its argument the constructor for BufferedReader accepts a InputStreamReader reference as its argument new BufferedReader ( ISR ) new FileInputStream ( file )

OOP&M - theory lectures25 OOP&M – come IN PUT files The total declaration would be: File f; FileInputStream fsr; InputStreamReader isr; BufferedReader bsr; f = new File(“Big_parties_many_potatis.txt”); fsr = new FileInputStream(f); isr = new InputStreamReader(fsr); bsr = new BufferedReader(isr);

OOP&M - theory lectures26 OOP&M – come IN PUT files Let’s read a couple of lines from the file and print them: File f; FileInputStream fsr; InputStreamReader isr; BufferedReader bsr; String inputline; f = new File(“Big_parties_many_potatis.txt”); fsr = new FileInputStream(f); isr = new InputStreamReader(fsr); bsr = new BufferedReader(isr); inputline = bsr.readLine(); System.out.println(inputline); inputline = bsr.readLine(); System.out.println(inputline);

OOP&M - theory lectures27 OOP&M – mixin’ it up Exercise: write a program that makes a copy of a file the name of the copy will be the same than the original plus “.copy” [Hint: consider the following flow of information for the program] [Hint2: it is obvious that you need to read the name of the file to be copied from the keyboard]

OOP&M - theory lectures28 FileInputStream InputStreamReader BufferedReader Some InputStream InputStreamReader BufferedReader CopyFile program PrintStream FileOutputStream OOP&M – mixin’ it up

OOP&M - theory lectures29 import java.io.*; class CopyFile { public static void main(String[] arg) throws Exception { InputStreamReader isrKeyboard; BufferedReader keyboard; String fileNameOrig; File fOrig; FileInputStream fisOrig; InputStreamReader isrOrig; BufferedReader rdrOrig; String fileNameCopy; File fCopy; FileOutputStream fosCopy; PrintStream psCopy; String s; isrKeyboard = new InputStreamReader(System.in); keyboard = new BufferedReader(isrKeyboard); System.out.print(“name of file to copy: “); System.out.flush(); fileNameOrig = keyboard.readLine(); fileNameCopy = fileNameOrig.concat(“.copy”); fOrig = new File(fileNameOrig); fisOrig = new FileInputStream(fOrig); isrOrig = new InputStreamReader(fisOrig); rdrOrig = new BufferedReader(isrOrig); fCopy = new File(fileNameCopy); fosCopy = new FileOutputStream(fCopy); psCopy = new PrintStream(fosCopy); s = rdrOrig.readLine(); psCopy.println(s); s = rdrOrig.readLine(); psCopy.println(s); … } models keyboard models file input models output to file access keyboard read name access original arrange to write to output read lines and write OOP&M – mixin’ it up