Introduction to Computing Using Java

Slides:



Advertisements
Similar presentations
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 2: Java Fundamentals Input and Output statements.
18 File handling1June File handling CE : Fundamental Programming Techniques.
Class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
File I/O (Input and Output). Why use files? Things stored in files are more permanent than things stored in variables in programs. Things stored in files.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
File Input/Output. 2Java Programming: From Problem Analysis to Program Design, 3e File Input/Output File: area in secondary storage used to hold information.
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
File and Stream I/O, Redirection Privacy and Super
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.6 Reading Input with java.io Produced by Harvey.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
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.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
CS1020 Data Structures and Algorithms I Lecture Note #16 File Processing.
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.:
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Introduction to programming in java
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
CSC 211 Java I File I/O and more methods. Today’s plan Homework discussion Reading lines from files Writing lines to files All of the above, using methods.
Basic Text File Input/Output
File - CIS 1068 Program Design and Abstraction
Text File Input/Output
Streams & File Input/Output (I/O)
Introduction to programming in java
File handling and Scanning COMP T1
Input/Output.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Streams and File I/O.
Hours question Given a file hours.txt with the following contents:
Text File Input/Output
User Input You’ve seen this: A Scanner object:
I/O Basics.
Java Programming: From Problem Analysis to Program Design, 4e
I/O Streams- Basics Byte Streams and Character Streams
INPUT STATEMENTS GC 201.
String Input ICS 111: Introduction to Computer Science I
CSS 161: Fundamentals of Computing
Introduction to Classes and Methods
Building Java Programs Chapter 6
Chapter 2: Basic Elements of Java
Chapter 2: Java Fundamentals
The keyboard is the standard input device.
Building Java Programs
CSC1401 Input and Output (with Files)
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Building Java Programs
Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program, then the stream.
Building Java Programs
Presentation transcript:

Introduction to Computing Using Java File and I/O 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK High Score Table 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Keep Information Non-volatile Modern computers are electronic. Without power supply  Data Loss! To keep data non-volatile, save it to a file! 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Retrieve Information Data on a file cannot be processed by a computer program directly. We have to load it from the file. Moreover, we don’t want to be typists who repeat the same data input every time. 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Background Information How does System.out.println() work? System is a class, what about System.out? Why TWO dots? 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Talk About Printing Before talking about input from keyboard, let’s first consider output to screen in Java. System.out.print() is a familiar message. System is a class. Just like Account. System has some fields, one of which is PrintStream out out is an object reference to an object of class PrintStream We further send a print() message to out 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Imagine...How Might It Look Like in Java? class System { // public class fields public static PrintStream out; public static InputStream in; // private class field example private static String computerName; // private instance field example private long memoryAvailable; ... } 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

The Picture System.out.print() PrintStream print( ) println( ) PrintStream print( ) println( ) X out in System.out is a PrintStream object reference. We then send a print() message to this object. InputStream … InputStream … 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

File and Input/Output Operations There are classes to represent a file on disk, a file in network, or input/ output from console. They provide methods to make file and IO operations more convenient. Class PrintStream and Class Scanner 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Class PrintStream import java.io.*; ...main(...) throws IOException { PrintStream myNewFile; myNewFile = new PrintStream("myWeb.txt"); myNewFile.println("Hello World in a file!"); System.out.println("Hello World on screen."); // myNewFile is a PrintStream object reference // System.out is a PrintStream object reference } 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

PrintStream Object References PrintStream anObjectRef; anObjectRef = System.out; anObjectRef.println("Say Hello to screen!"); anObjectRef = new PrintStream("myWeb.txt"); anObjectRef.println("Say Hello to a file!"); // anObjectRef is a variable. At different time, // it refers to different PrintStream objects: // // System.out is a PrintStream object // new PrintStream() creates another object 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

PrintStream Object References PrintStream anObjectRef; anObjectRef = System.out; anObjectRef.println("Say Hello to screen! "); PrintStream myNewFile; myNewFile = new PrintStream("myWeb.txt"); anObjectRef = myNewFile; anObjectRef.println("Say Hello to a file! "); // we can copy object references // anObjectRef.println will print differently 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Class Scanner import java.util.*; import java.io.*; ...main(...) throws Exception { Scanner markFile; markFile = new Scanner(new File("myWeb.txt")); int mark; if (markFile.hasNextInt()) mark = markFile.nextInt(); } 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Class Scanner The methods hasNextInt(), hasNextDouble(), hasNextXyz()… return us a boolean (true/ false) value that indicates if there is more data to read from the Scanner object. The methods nextInt(), nextDouble(), … reads a piece of data from the source for us. Operations may fail, thus we add “throws Exception” to the main() method. 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Class Scanner: line-by-line import java.util.*; import java.io.*; ...main(...) throws Exception { Scanner markFile; markFile = new Scanner(new File("myWeb.txt")); String aLine; while (markFile.hasNextLine()) aLine = markFile.nextLine(); System.out.println(aLine); } 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Class Scanner: line-by-line The method nextLine() reads a line from the source for us. It returns a String. The source for the Scanner object could be the keyboard, a file, a web source. System.in is a field in class System. It refers to a default InputStream object, representing the keyboard. 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Class Scanner The source could be the keyboard object new Scanner( System.in ); a file object new Scanner( new File(“filename”) ); a web source new Scanner(new URL(“http://...”).openStream( ) ); 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK End Note Readings and References Section 2.6 The Scanner class from Java 2005-2008 5b Michael Fung, CS&E, The Chinese University of HK