Introduction to programming in java

Slides:



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

Computer Programming Lab(7).
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.
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
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.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements 3.6 – Iterators – p
Computer Programming Lab 8.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
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 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
File I/O There’s more to life than the keyboard. Interactive vs. file I/O All of the programs we have seen or written thus far have assumed interaction.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
11 Chapter 4 LOOPS AND FILES CONT’D. 22 SENTINEL-CONTROLLED LOOPS Suppose we did not know the number of grades that were to be entered. Maybe, a single.
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.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
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.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
1 BUILDING JAVA PROGRAMS CHAPTER 6 FILE PROCESSING.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
Chapter 15 Text Processing and File Input/Output Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
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.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: 6.1 – 6.2, 5.4.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
For Friday Finish reading chapter 9 WebCT quiz 17.
Introduction to programming in java Lecture 26 Writing in a file.
Basic Text File Input/Output
Text File Input/Output
CMSC 202 Text File I/O.
Reading from a file and Writing to a file
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
Chapter 1 Introduction to Java
Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use.
OBJECT ORIENTED PROGRAMMING II LECTURE 2 GEORGE KOUTSOGIANNAKIS
Building Java Programs
Strings and File I/O.
Maha AlSaif Maryam AlQattan
Text File Input/Output
Interactive Standard Input/output
COMPUTER 2430 Object Oriented Programming and Data Structures I
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Lecture Note Set 1 Thursday 12-May-05
I/O Basics.
Computer Programming Methodology File Input
Testing and Exceptions
Outline Boolean Expressions The if Statement Comparing Data
Java for Teachers Intermediate
Introduction to Classes and Methods
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
Unit 6 Working with files. Unit 6 Working with files.
Know for Quiz Everything through Last Week and Lab 7
File Handling in Java January 19
OBJECT ORIENTED PROGRAMMING II LECTURE 13_1 GEORGE KOUTSOGIANNAKIS
Presentation transcript:

Introduction to programming in java Lecture 25 Input from file

Input Stream from a File We can also construct a scanner object that connects to a disk file: File file = new File("myData.txt"); // create a File object Scanner scan = new Scanner( file ); // connect a Scanner to the file

Program with Disk Input The program reads its data from myData.txt, a text file that might have been created with a text editor. The file starts with an integer in character format. The Scanner scans over the beginning spaces until it finds characters that can be converted to int. It stops scanning when it encounters the first space after the digits it is converting.

IOException The file myData.txt should exist and should be in the same directory as the program. If the file does not exist, the program cannot construct a Scanner that reads from it. If this happens, the run-time Java system will throw an IOException. Because of this, two things are needed in the program. The main() method must start with the line: public static void main (String[] args) throws IOException And the Java io package must be imported: import java.io.*;

hasNextInt() Most files contain a great deal of data. Practical programs must process this data using a loop of some sort. Here is a program that reads a text file that contains many integers and writes the square of each one.

Example Run For example, myData.txt contains following data.

HasNext Methods

User Names the File

Example Run

Practice Question Write a program that adds all the integers in a file of text integers. Prompt the user for the name of the input file. Write a program which prints the data of whole file.

Question 1

Question 2