Lecture 13: Keyboard Input and Text Files Yoni Fridman 7/23/01 7/23/01.

Slides:



Advertisements
Similar presentations
25-Aug-14 Simple Java I/O Part I General Principles.
Advertisements

Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
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.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Chapter 2 Review Questions
Lab#1 (14/3/1431h) Introduction To java programming cs425
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
Tim Wentz Nathaniel Smith Andrew Johnson. Files in Java Create a new file handle using: ▫new File(String pathname); Pathnames can be either relative or.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
Simple Java I/O Part I General Principles. 2 Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Files and Streams. Goals To be able to read and write text files To be able to read and write text files To become familiar with the concepts of text.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input.
Lecture 12: Using Classes Yoni Fridman 7/19/01 7/19/01.
Lecture 3: Strings, Screen Output, and Debugging Yoni Fridman 7/2/01 7/2/01.
Class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat.
Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
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 ->
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
15-Jul-15 Simple Java I/O Part I General Principles.
1 Course Lectures Available on line:
Two Ways to Store Data in a File Text format Binary format.
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.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
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.
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.
MIT AITI 2003 Lecture 15 Streams Input and Output data from/to other sources.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
File IO Basics By Dan Fleck Coming up: Data Streams.
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
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.
Read and Write Files  By the end of this lab you will be able to:  Write a file in internal storage  Read a file from internal storage  Write a file.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
ITEC 109 Lecture 7 Operations. Review Variables / Methods Functions Assignments –Purpose? –What provides the data? –What stores the data? –What type of.
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.
CSCI 1100/1202 January 23, Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.
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.
Object Oriented Programming Object and Classes Lecture 3 MBY.
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.
© A+ Computer Science - import java.util.Scanner; Try to be as specific as possible when using an import.
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 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.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
I/O in java and Revision. 2 I/O in Java Many packages and libraries associated with Java provide sophisticated ways to input and output information, e.g.:
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Introduction to programming in java
Interactive Standard Input/output
Lecture Note Set 1 Thursday 12-May-05
I/O Basics.
Part I General Principles
JAVA IO.
Reading and Writing Text Files
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
File Input and Output.
Data Types and Maths Programming Guides.
Chapter 8: Exceptions and I/O Streams
Presentation transcript:

Lecture 13: Keyboard Input and Text Files Yoni Fridman 7/23/01 7/23/01

OutlineOutline ä Overview ä Input classes ä InputStreamReader ä BufferedReader ä Reading keyboard input ä Reading Strings ä Converting to other types ä Reading from text files ä Overview ä Input classes ä InputStreamReader ä BufferedReader ä Reading keyboard input ä Reading Strings ä Converting to other types ä Reading from text files

OverviewOverview ä A stream is a technical term for a whole sequence of text input. (Think of it as a stream of characters.) ä Whenever Java reads input (from the keyboard, from a file, etc.), it takes in a stream and stores it in what’s called a Buffer: ä A Buffer is an object that holds each of the characters read in from a stream. To be of any use to us, the Buffer must be converted to a String. ä A stream is a technical term for a whole sequence of text input. (Think of it as a stream of characters.) ä Whenever Java reads input (from the keyboard, from a file, etc.), it takes in a stream and stores it in what’s called a Buffer: ä A Buffer is an object that holds each of the characters read in from a stream. To be of any use to us, the Buffer must be converted to a String. Hello

Input Classes ä Java has two classes that we’ll need to use to read input from the keyboard:  InputStreamReader is a class that’s used to read streams from the keyboard and save them as Buffers.  BufferedReader is a class that’s used to convert a Buffer to a String. ä These classes aren’t automatically available for our use.  To use them, we need the following command at the very beginning of our program: import java.io.*;  import is a keyword that tells Java to load certain classes for our use. ä Java has two classes that we’ll need to use to read input from the keyboard:  InputStreamReader is a class that’s used to read streams from the keyboard and save them as Buffers.  BufferedReader is a class that’s used to convert a Buffer to a String. ä These classes aren’t automatically available for our use.  To use them, we need the following command at the very beginning of our program: import java.io.*;  import is a keyword that tells Java to load certain classes for our use.

Reading Keyboard Input  Before we can read input from the keyboard, we must create a new object of the InputStreamReader class, like this: InputStreamReader stream = new InputStreamReader(System.in);  Then we must create a new object of the BufferedReader class, like this: BufferedReader buffer = new BufferedReader(stream);  Finally, we can read input using the instance method readLine() of the class BufferedReader : ä String input; input = buffer.readLine();  Before we can read input from the keyboard, we must create a new object of the InputStreamReader class, like this: InputStreamReader stream = new InputStreamReader(System.in);  Then we must create a new object of the BufferedReader class, like this: BufferedReader buffer = new BufferedReader(stream);  Finally, we can read input using the instance method readLine() of the class BufferedReader : ä String input; input = buffer.readLine();

Converting to Other Types ä Input is always read into Strings. Even if we enter the number 3, Java stores the String “3”.  We can convert to an int like this: ä int inputInt; inputInt = Integer.parseInt(input);  And we can convert to a double like this: ä double inputDouble; ä inputDouble = Double.valueOf(input).doubleValue(); ä Input is always read into Strings. Even if we enter the number 3, Java stores the String “3”.  We can convert to an int like this: ä int inputInt; inputInt = Integer.parseInt(input);  And we can convert to a double like this: ä double inputDouble; ä inputDouble = Double.valueOf(input).doubleValue();

Reading from Text Files ä What if we have text (or any data) that has been stored in a file? ä We can read that as input much like we read keyboard input. ä There’s another class that we’ll need to read input from a text file:  FileReader is a class that’s very similar to InputStreamReader – it’s used to read streams from a text file. ä What if we have text (or any data) that has been stored in a file? ä We can read that as input much like we read keyboard input. ä There’s another class that we’ll need to read input from a text file:  FileReader is a class that’s very similar to InputStreamReader – it’s used to read streams from a text file.

Reading from Text Files  Instead of creating an InputStreamReader object, we create a FileReader object, like this: FileReader file = new FileReader(“data.txt”); ä In this case, data.txt is the name of our text file – this can be any name we want it to be.  Now, we can use the FileReader object file just like we used the InputStreamReader object stream before: ä BufferedReader buffer = new BufferedReader(file); String input = buffer.readLine();  Instead of creating an InputStreamReader object, we create a FileReader object, like this: FileReader file = new FileReader(“data.txt”); ä In this case, data.txt is the name of our text file – this can be any name we want it to be.  Now, we can use the FileReader object file just like we used the InputStreamReader object stream before: ä BufferedReader buffer = new BufferedReader(file); String input = buffer.readLine();

HomeworkHomework ä Read: 13.4, to the bottom of page 568. ä HW6 out today, due Friday. ä Calculator. ä Read: 13.4, to the bottom of page 568. ä HW6 out today, due Friday. ä Calculator.