File Input / Output.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 11.  Data is often stored in files such as a text file  We need to read that data into our program  Simplest mechanism  Scanner class  First.
CIS 1068 Program Design and Abstraction
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.
Files and Streams CS 21a Chapter 11 of Horstmann.
CIS 234: File Input & Output Dr. Ralph D. Westfall May, 2007.
18 File handling1June File handling CE : Fundamental Programming Techniques.
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.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Files Section 2.13, Section 8.8.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
1 BUILDING JAVA PROGRAMS CHAPTER 6 FILE PROCESSING.
File Input/Output. 2Java Programming: From Problem Analysis to Program Design, 3e File Input/Output File: area in secondary storage used to hold information.
Lecture 2 Objectives Learn about objects and reference variables.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
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.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Lab 04-2 Objectives:  Understand what a file is.  Learn how to use the File class  Learn how to use the Scanner class to read from files  Learn about.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
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.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
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.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
OO Design and Programming II I/O: Reading and Writing
Basic Text File Input/Output
File - CIS 1068 Program Design and Abstraction
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
Lesson 8: More File I/O February 5, 2008
CS 160 Final Review.
Java Programming Lecture 2
File handling and Scanning COMP T1
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.
Streams and File I/O.
Program Input/Output (I/O)
Building Java Programs
Strings and File I/O.
Week 14 - Wednesday CS 121.
I/O Basics.
Creating and Modifying Text part 2
Some File I/O CS140: Introduction to Computing 1 Savitch Chapter 9.1, 10.1, /25/13.
Working with files.
Advanced Java Programming
Streams and File I/O Chapter 14.
Chapter 3: Introduction to Objects and Input/Output
CSS 161: Fundamentals of Computing
Building Java Programs Chapter 6
Building Java Programs Chapter 6
Unit 6 Working with files. Unit 6 Working with files.
Input/output (I/O) import java.io.*;
Building Java Programs
File Handling in Java January 19
Building Java Programs
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Building Java Programs
File Input and Output.
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.
Presentation transcript:

File Input / Output

Files Persistent storage So your application can ‘remember’ Similar methods to reading / displaying information as before, but a different ‘destination’

Scanner Class Before, we read data from ‘standard input’ Scanner myScanner = new Scanner (System.in); System.in will read from the keyboard We can pass Scanner other Objects to read from other sources…

Scanner Class Now we use the FileReader class Don’t forget your imports java.util java.io Scanner myScanner = new Scanner (new FileReader(“c:\\myfolder\\myFile.txt”);

FileReader Scanner myScanner = new Scanner (new FileReader(“c:\\myfolder\\myFile.txt”); Some Things to notice: We create a FileReader Object “on the fly” It doesn’t even have a name The double slashes Slash is the escape character Paths Relative vs. Absolute Drive letters

FileReader Now we can use the same scanner methods nextInt( ) nextDouble( ) hasNext( )

Example Scanner myScanner = new Scanner (new FileReader(“c:\\myFile.txt”)); String firstName, lastName; double hoursWorked, payRate, wages; firstName = myScanner.next( ); lastName = myScanner.next( ); hoursWorked = myScanner.nextDouble( ); payRate = myScanner.nextDouble( ); wages = hoursWorked * payRate; myScanner.close( );

Writing to Files Lots of different “Writer and Reader” classes. PrintWriter is a good, high level class for reading general data from a file

Writing to Files PrintWriter myPW = new PrintWriter(“c:\\myFile.out”); Can use the familiar output methods on the file print( ) println( ) myPW.println(“The pay is: $” + pay); This stores the text in the file “myFile.out”

Closing Files Don’t forget to close your files! Saves memory and… “Flushes the buffer”

Files What happens if something goes wrong? Trying to read a file that isn’t there… Trying to write to a location that doesn’t exist… Like the ‘Z:’ drive Trying to write to a file that is already there… By default, it overwrites the file. This may not be what you want!

Files If something goes wrong, the statement that causes a problem throws an exception One of two things must happen in a method that causes an exception You deal with it Or… pass the problem up to the calling method

Exceptions For now, we will ‘pass the buck’ and let the system deal with the potential error public static void main (String [] args) throws FileNotFoundException { . . . // no try – catch block in here }

Files In the real-world, files are useful, but many real world data tasks are handled via a database mySQL Websites use a database to hold Products Comments Reviews