File Input and Output CSC 1401: Introduction to Programming with Java Week 4 – Lecture 2 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Advertisements

Lecture 2 Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
Converting Between Numbers and Characters CSC 1401: Introduction to Programming with Java Week 4 – Lecture 3 Wanda M. Kunkle.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Repetition Structures: Do-while Loop Random Number Generation CSC 1401: Introduction to Programming with Java Week 6 Wanda M. Kunkle.
Data Input and Output Using the Library Software CSC 1401: Introduction to Programming with Java Lecture 4 – Part 1 Wanda M. Kunkle.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle.
Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 11, Lecture 2 (Wed)
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Introduction to Computer Systems and the Java Programming Language.
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
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.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Pascal Programming Today Chapter 11 1 Chapter 11.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Computer Programming Control Structure
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Chapter 14: Sequential Access Files
REPETITION CONTROL STRUCTURE
CiS 260: App Dev I Chapter 4: Control Structures II.
Ch 7: JavaScript Control Statements I.
Repetition Structures (Loops)
Chapter 5: Control Structures II
CSC115 Introduction to Computer Programming
Alternate Version of STARTING OUT WITH C++ 4th Edition
Today’s Lecture I/O Streams Tools for File I/O
CPS125 Week
Functions continued.
Reading from and Writing to Files
Reading from and Writing to Files
Presentation transcript:

File Input and Output CSC 1401: Introduction to Programming with Java Week 4 – Lecture 2 Wanda M. Kunkle

2 Why Use Files for Input/Output (I/O)? Data input at the keyboard and output to the screen is only stored temporarily. (At least that has been the case in the Java programs we have worked with so far.) Data input at the keyboard and output to the screen is only stored temporarily. (At least that has been the case in the Java programs we have worked with so far.) To store data permanently, we must write it to a file. (This is similar to creating an MS Word document and saving it under a specific file name.) To store data permanently, we must write it to a file. (This is similar to creating an MS Word document and saving it under a specific file name.)

3 Different Types of Files Data can be stored in one of two file types: Data can be stored in one of two file types: –Text files (called sequential files in your book) Sequences of characters Sequences of characters Can be read by human beings Can be read by human beings Can read and write to them using a text editor (e.g., Notepad) Can read and write to them using a text editor (e.g., Notepad) –Binary files Sequences of binary digits Sequences of binary digits Can only be read by computer programs Can only be read by computer programs Can only read and write to them using a computer program (e.g., a Java program) Can only read and write to them using a computer program (e.g., a Java program)

4 What Types of Files Will We Be Reading and Writing? Text files Text files –The kind that human beings can read –The kind that human beings can read

5 Sample Text File: MovieQuotes.txt Data: Strange. Part of me is sorry that she is dead. She brought me closer to humanity than I ever thought possible. And for a time I was tempted by her offer. Picard: How long a time? Data: Zero point eight six seconds....For an android that is nearly an eternity. Data: Strange. Part of me is sorry that she is dead. She brought me closer to humanity than I ever thought possible. And for a time I was tempted by her offer. Picard: How long a time? Data: Zero point eight six seconds....For an android that is nearly an eternity.

6 Sample Text File: MovieQuotes.txt Any idea what Star Trek movie the quotes are from? Any idea what Star Trek movie the quotes are from? But I digress … ;-) But I digress … ;-)

7 Steps to Read a Text File Open the file for input Open the file for input –This step positions the file pointer at the first character in the file. –The file pointer is a hidden variable that defines the position of the next input character in the file. Read the file character by character until the eof (end-of-file) character is encountered Read the file character by character until the eof (end-of-file) character is encountered –The eof character is a special character that marks the end of the file. –It is visible to computers but not to humans. Close the file Close the file

8 Methods for Reading a Text File in Java There are three provided by the software that comes with our book: There are three provided by the software that comes with our book: –in.read() –in.next() –in.more()

9 Methods for Reading a Text File in Java in.read() in.read() –Reads the next input character (or basic.eof ) –Moves the file pointer one character ahead –Example: // Read a single character and // advance file pointer char inputChar = in.read();

10 Methods for Reading a Text File in Java in.next() in.next() –Reads the next input character (or basic.eof ) –Does NOT advance the file pointer –Example: // Read a single character but // do not advance the file pointer char inputChar = in.next();

11 Methods for Reading a Text File in Java in.more() in.more() –Returns a true or false value –True if there is more input to be read from the file; false, otherwise –Returns the same value as: in.next() != basic.eof –Example: // Read the file character by character // as long as there is input to be read while (in.more()) { char inputChar = in.read(); }

12 Repetition Structures while while –Usage: Used to specify that an action is to be repeated as long as some condition remains true Used to specify that an action is to be repeated as long as some condition remains true May never execute if the result of the initial test is false May never execute if the result of the initial test is false –Format: while (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } –Format: while (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } –Example: int counter = 0; while (counter < 100) { // Loops as long as counter is less than 100 out.writeln(counter); // Display counter counter = counter + 1; // Increment counter by 1 }

13 Sample Programs Now let’s look at some sample programs that demonstrate reading text files: Now let’s look at some sample programs that demonstrate reading text files: –InputTextFile1.java InputTextFile1.java –InputTextFile2.java InputTextFile2.java

14 Steps to Write a Text File Open a file for output (e.g., “Out.txt”) Open a file for output (e.g., “Out.txt”) –Creates an empty file named “Out.txt,” if a file with that name does not exist –Overwrites the file named “Out.txt,” if a file with that name does exist Write text to the file character by character until "eof" (for end-of-file) is entered Write text to the file character by character until "eof" (for end-of-file) is entered Close the file Close the file

15 Sample Programs Now let’s look at a sample program that demonstrates writing a text file: Now let’s look at a sample program that demonstrates writing a text file: –OutputTextFile1.java OutputTextFile1.java

16 Steps to Copy a Text File Open one file for input (e.g., MovieQuotes.txt) Open one file for input (e.g., MovieQuotes.txt) Open a second file for output (e.g., CopyOfMovieQuotes.txt) Open a second file for output (e.g., CopyOfMovieQuotes.txt) Read the input file character by character until the eof (end-of-file) character is encountered Read the input file character by character until the eof (end-of-file) character is encountered Write the output file character by character as the input file is read Write the output file character by character as the input file is read Close the input file Close the input file Close the output file Close the output file

17 Sample Programs Now let’s look at a sample program that demonstrates copying one text file to another text file: Now let’s look at a sample program that demonstrates copying one text file to another text file: –CopyTextFile1.java CopyTextFile1.java