OOP&M - laboratory lectures1 OOP&M – LAB2 LABzwei: the Input.

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
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
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.
Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
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.
OOP&M - theory lectures1 OOP&M – the second day “… In the beginning God created the heavens and the earth …” - the Hollybook -
Files and Streams CS 21a Chapter 11 of Horstmann.
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.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
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 ->
Java Review 2. The Agenda The following topics were highlighted to me as issues: –File IO (Rem) –Wrappers (Rem) –Interfaces (Rem) –Asymptotic Notation.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
OOP&M - laboratory lectures1 JPN – GUI and Applets “… I am sure ‘it wont fail as much as the one before …” - Bill Gates about W98 -
Variable Scope Brackets, brackets…. brackets. Variable Scope /** * HelloWorld --- program that prints something to the screen. Blanca Polo */
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Io package as Java’s basic I/O system continue’d.
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
1 Course Lectures Available on line:
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
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.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
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 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
File IO Basics By Dan Fleck Coming up: Data Streams.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
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.
Files Tutor: You will need ….
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.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
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.
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.
CSI 3125, Preliminaries, page 1 Files. CSI 3125, Preliminaries, page 2 Reading and Writing Files Java provides a number of classes and methods that allow.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
CHAPTER 3 File Output.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Getting Started – Basic Page Structure
OO Design and Programming II I/O: Reading and Writing
Introduction to programming in java
Program Input/Output (I/O)
Interactive Standard Input/output
I/O Basics.
I/O Streams- Basics Byte Streams and Character Streams
Operators Laboratory /11/16.
Reading and Writing Text Files
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
File Handling in Java January 19
I/O and Applet from Chapter 12
Java Input/Output (I/O)
Exception Handling Contents
File Input and Output.
Presentation transcript:

OOP&M - laboratory lectures1 OOP&M – LAB2 LABzwei: the Input

OOP&M - laboratory lectures2 OOP&M – LAB2 – connection types output input Program file monitor We have studied the standard connections of our computer to the rest of the world During LAB1 we learned how to show information on the screen or how to save it into a file Today we will learn in which way the information can be taken from files or from the keyboard in order to improve the features of our programs Finally we will see how to get information from the HTML files from the internet... we will program our first browser file keyboard internet

OOP&M - laboratory lectures3 OOP&M – LAB2 – do you remember? Connections between Java programs and the rest of the world are made using streams Streams are like “bridges” of information that link Java programs and the screen, the keyboard, a file or the net Streams are just sequences of bytes... numbers that we cannot understand at a first sight Therefore we need some classes which help us to transform the information into something that we could read First we transform the numbers into long sequences of characters Second we transform the characters into strings At the end, the way in which we can handle the information is very easy. Methods like print, println, readLine and others make these processes easier

OOP&M - laboratory lectures4 OOP&M – LAB2 LABzwei: the Input Keyboard

OOP&M - laboratory lectures5 OOP&M – LAB2 – keyboard The use of the keyboard will improve the features of the programs that we made during the first lab-lesson Now we can ask to the user which files should be deleted, or which line should be printed to the screen An easy example is: import java.io.*; public class read { public static void main(String arg[]) throws Exception { InputStreamReaderisr; BufferedReaderkeyb; Stringline; isr = new InputStreamReader(System.in); keyb = new BufferedReader(isr); line = keyb.readLine(); System.out.println(line); }

OOP&M - laboratory lectures6 OOP&M – LAB2 – keyboard EXERCISE A.1 - Improving DeleteFile Take the following program (a version from “ DeleteFile.java ”) and make it ask the user which file should be deleted: import java.io.*; public class DeleteFile { public static void main(String arg[]) throws Exception { File f; f = new File("to_be_removed.java"); f.delete(); System.out.println("File deleted"); } [Hint: use *.flush() for showing the information]

OOP&M - laboratory lectures7 EXERCISE A.2 - Counting things Implement a program that reads a line from the keyboard and answers how many “o’s” are in it [Hint: use methods like *.equals(), *.toUpperCase() or *.substring() from the String class for searching] OOP&M – LAB2 – monitor

OOP&M - laboratory lectures8 EXERCISE A.3 - Non-stop counting Improve program A.2 in order to make it loop until the user enters a blank line [Hint: use the while loop with a structure like: s = keyb.readLine(); while (s.length() > 0) {... s = keyb.readLine(); } ] OOP&M – LAB2 – monitor This checks if the string had a length of at least one character

OOP&M - laboratory lectures9 OOP&M – LAB2 LABzwei: the Input Files

OOP&M - laboratory lectures10 OOP&M – LAB2 – files Now we are going to try to read information from files During the theory lecture we saw that files communicate with programs using sequences of bytes, called Streams After that we saw that we could use other structures for reading text from the files in a very easy way The constructors were: Strings; File fOrig; FileInputStream fisOrig; InputStreamReader isrOrig; BufferedReader rdrOrig; fOrig = new File(“data.input”); fisOrig = new FileInputStream(fOrig); isrOrig = new InputStreamReader(fisOrig); rdrOrig = new BufferedReader(isrOrig); s = rdrOrig.readLine();

OOP&M - laboratory lectures11 OOP&M – LAB2 – files EXERCISE B.1 – Files, how to print them Create a text-file with the Notepad and save it in the same directory where you are working Write a program called “PrintFile” that prints a couple of lines of the text file on the screen [Hint: remember that the way in which we print is with System.out.println() ]

OOP&M - laboratory lectures12 OOP&M – LAB2 – files EXERCISE B.2 – Files, how to copy them Create a text-file with the Notepad and save it in the same directory where you are working Write a program called “CopyFile” that makes a copy of it called “*.copy” (the same name and extension plus “.copy”) After copying the file write a message on the screen that indicates the end of the action [Hint: notice that here we work with two files. We need two file objects, one with an InputStream and the other one with an OutputStream] [Hint2: use a while loop like: s = rdrOrig.readLine(); while (s != null) {... s = rdrOrig.readLine(); } ]

OOP&M - laboratory lectures13 OOP&M – LAB2 – files EXERCISE B.3 – Files, how to search through them Write a program called “LookFor” that asks for the user the name of a file Afterwards, it will ask for a string to search As a result the program will answer in how many lines it found the string in the file and print the lines [Hint: this exercise is a mixture of A.2 and B.2... It is difficult, but not so much!!] [Hint2: you need to use *.indexOf(string_to_search ) that answers “-1” if the string_to_search is not found]

OOP&M - laboratory lectures14 OOP&M – LAB2 LABzwei: the Input Internet

OOP&M - laboratory lectures15 OOP&M – LAB2 – internet Now we are going to try to read information from the Web The Net as other communication media for our programs can be read using Streams There is a difference between what we saw until now and what will happen with the Net: files are in HTML code The constructors are: Strings; URL u; InputStream ins; InputStreamReader isr; BufferedReader link; u = new URL(" ins = u.openStream(); isr = new InputStreamReader(ins); link = new BufferedReader(isr); s = link.readLine(); As you see here, the structure is similar to the one that we saw for the files. The difference is at the constructor for the InputStream object, which is special!! But the method for reading the information is the same: *.readline()

OOP&M - laboratory lectures16 OOP&M – LAB2 – internet EXERCISE C.1 – Just reading a file Implement the next program and answer: A)how does it work? B)how does the information look like? C)where is the difference/s to a Graphical Browser? D)what would you do for improving your text browser and update it to “MySecondBrowser”? (do not be too ambitious, please)

OOP&M - laboratory lectures17 import java.net.*; import java.io.*; class MyFirstBrowser { public static void main(String[] arg) throws Exception { URL uNet; InputStream insNet; InputStreamReader isrNet; BufferedReader linkNet; String s; uNet = new URL(" insNet = uNet.openStream(); isrNet = new InputStreamReader(insNet); linkNet = new BufferedReader(isrNet); s = linkNet.readLine(); System.out.println(s); s = linkNet.readLine(); System.out.println(s); System.out.println("connection closed"); } OOP&M – LAB2 – internet EXERCISE C.1 – Just reading a file

OOP&M - laboratory lectures18 OOP&M – LAB2 – internet EXERCISE C.2 – Look for the title and the body Write the program called “Browser” that asks for the user the name of URL As a result the program will answer with the title of the file and will print the text contained into the “body” part of the HTML file [Hint: the title is to be found between the and TAGs in a HTML file, and the body between and ]

OOP&M - laboratory lectures19 Do it yourself: try to finish your own browser, add features like: jump to the next line with and underline a text if it is a link ( ) show the name of the images ( ) try to show tables (...) think how to ask to the user for the next jump to do... [NOTE: this exercise is for doing it at home, start if you have the time during the LAB, but continue as far as you can... Look for your preferred solution to all this problems] OOP&M – LAB2 – internet EXERCISE C.3 – Extra work