September 9, 2008 Lecture 5 – More IO.

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
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 CS 21a Chapter 11 of Horstmann.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
18 File handling1June File handling CE : Fundamental Programming Techniques.
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.
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.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Exception Handling (Chapter 8) CS 180 Recitation - February 29, 2008 Department of Computer Science Purdue University.
LAB 10.
2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Announcements Quiz 2 Grades Posted on blackboard.
Computer Programming Lab(5).
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.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.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Introduction to Programming G50PRO University of Nottingham Unit 11 : Files Input/Ouput Paul Tennent
Input and Output Using Text Files and Exception Handling.
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
1 Simple Input Interactive Input - The Class Scanner n Command-Line Arguments.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
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.
For Friday Finish reading chapter 9 WebCT quiz 17.
File I/O CLI: File Input CLI: File Output GUI: File Chooser
Introduction to programming in java
Input/Output.
TemperatureConversion
CS 240 – Computer Programming I Lab
Compiling and Running a Java Program
Strings and File I/O.
Maha AlSaif Maryam AlQattan
Computer Programming Methodology Input and While Loop
Introduction to Methods in java
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Something about Java Introduction to Problem Solving and Programming 1.
Some File I/O CS140: Introduction to Computing 1 Savitch Chapter 9.1, 10.1, /25/13.
הרצאה 12: קבצים וחריגות (Exceptions)
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Introduction to Classes and Methods
Reading and Writing Text Files
A+ Computer Science INPUT.
September 9, 2008 Lecture 5 – More IO.
Introduction to Java Brief history of Java Sample Java Program
CSE Module 1 A Programming Primer
Building Java Programs
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Optional Topic: User Input with Scanner
Presentation transcript:

September 9, 2008 Lecture 5 – More IO

Alternatives Given: FileReader myFreader; One way Another way myFreader = new FileReader(“MyFile.txt”); Another way String fileName; fileName = “MyFile.txt”; myFreader = new FileReader(fileName);

Alternatives Scanner fileScanner; fileScanner = new Scanner (new File (“a.txt”));

Alternatives Scanner fileScanner; String fileName; fileName = “a.txt”; fileScanner = new Scanner (new File (fileName));

Alternatives Scanner fileScanner; File myFile; String fileName; fileName = “a.txt” // get from user myFile = new File (fileName); fileScanner = new Scanner (myFile);

Alternatives FileWriter fwriter; fwriter = new FileWriter (“b.txt”); String fileName; fileName = “b.txt”; Fwriter = new FileWriter (fileName);

Alternatives PrintWriter myPrinter; myPrinter = new PrintWriter (new FileWriter (“b.txt”));

Example import java.util.Scanner; public class Test { public static void main (String args []) { int number; Scanner keyboard; keyboard = new Scanner (System.in); System.out.println (" Hello "); System.out.println (" Please enter an integer and hit return "); while ( keyboard.hasNext()) { number = keyboard.nextInt (); System.out.println (" You entered " + number); } System.out.println (" Goodbye "); } }

Windows Screen Shot U:\Web\CS239\Lectures\Lecture5>java Test Hello Please enter an integer and hit return 65 You entered 65 32 You entered 32 -235 You entered -235 ^Z Goodbye ^D Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at Test.main(Test.java:14) 53 You entered 53 U:\Web\CS239\Lectures\Lecture5>

Alternatives PrintWriter myPrinter; FileWriter myWriter; myWriter = new FileWriter (“b.txt”); myPrinter = new PrintWriter (myWriter);

Alternatives PrintWriter myPrinter; FileWriter myWriter; String fileName; fileName = “b.txt”; myWriter = new FileWriter (fileName); myPrinter = new PrintWriter (myWriter);

Alternatives End of file character Unix Windows <ctrl-d> <ctrl-z>