Some File I/O CS140: Introduction to Computing 1 Savitch Chapter 9.1, 10.1, /25/13.

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 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
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.
10-1 Writing to a Text File When a text file is opened in this way, a FileNotFoundException can be thrown – In this context it actually means that the.
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.
CIS 1068 Program Design and Abstraction
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
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.
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
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.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
Introduction to Programming G50PRO University of Nottingham Unit 11 : Files Input/Ouput Paul Tennent
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.
Chapter 10 Text Files Section 10.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
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.
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.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
GENERICS AND FILE HANDLING Saumya Srivastava (977934) Divyangana Pandey (977790) Shubhi Saxena (978108) Arka Das (962969) AHD05/15-16 AJA 21.
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.
Introduction to Exceptions in Java CS201, SW Development Methods.
For Friday Finish reading chapter 9 WebCT quiz 17.
CHAPTER 3 File Output.
File I/O (Input and Output)
File - CIS 1068 Program Design and Abstraction
File Input / Output.
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
Chapter 12 Exception Handling And Text IO
Streams and File I/O.
Introduction to Exceptions in Java
Introduction to Exceptions in Java
Peer Instruction 9 File Input/Output.
Week 14 - Wednesday CS 121.
I/O Basics.
Testing and Exceptions
CSS161: Fundamentals of Computing
CS Week 11 Jim Williams, PhD.
Exceptions & exception handling
Advanced Java Programming
04/14/14 Exceptions.
Streams and File I/O Chapter 14.
Exceptions & exception handling
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
CSS 161: Fundamentals of Computing
September 9, 2008 Lecture 5 – More IO.
File I/O ICS 111: Introduction to Computer Science I
September 9, 2008 Lecture 5 – More IO.
CSC1401 Input and Output (with Files)
Chapter 12 Exception Handling and Text IO Part 1
Chapter 11 Exceptions Java Software Solutions
Basic Exception Handling
CIS 110: Introduction to Computer Programming
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:

Some File I/O CS140: Introduction to Computing 1 Savitch Chapter 9.1, 10.1, /25/13

Exceptions (Chapter 9.1) Exceptions help you take care of unexpected things, divide the program into normal case and a special case Exceptions are objects Exceptions are thrown Exceptions are handled 2

Exceptions Scanner keyboard = new Scanner( System.in ); int numerator = keyboard.nextInt(); int denominator = keyboard.nextInt(); double fraction = (double) numerator / denominator ; System.out.println( fraction ); System.out.println( “End of program” ); 3

Exceptions Scanner keyboard = new Scanner( System.in ); int numerator = keyboard.nextInt(); int denominator = keyboard.nextInt(); double fraction; if ( denominator != 0 ) { fraction = (double) numerator / denominator ; System.out.println( fraction ); } else { System.out.println( “You can’t divide by zero!” ); System.out.println( “Go learn about division!” ); } System.out.println( “End of program” ); 4

Scanner keyboard = new Scanner( System.in ); int numerator = keyboard.nextInt(); int denominator = keyboard.nextInt(); double fraction; try { if ( denominator == 0 ) throw new Exception( “Exception! You can’t divide by zero”); fraction = (double) numerator / denominator ; System.out.println( fraction ); } catch ( Exception e ) { System.out.println( e.getMessage() ); System.out.println( “Go learn about division!” ); } System.out.println( “End of program” ); 5

Scanner keyboard = new Scanner( System.in ); int numerator = keyboard.nextInt(); int denominator = keyboard.nextInt(); double fraction; try { if ( denominator == 0 ) throw new Exception( “Exception! You can’t divide by zero”); fraction = (double) numerator / denominator ; System.out.println( fraction ); } catch ( Exception e ) { System.out.println( e.getMessage() ); System.out.println( “Go learn about division!” ); } System.out.println( “End of program” ); 6 try block

Scanner keyboard = new Scanner( System.in ); int numerator = keyboard.nextInt(); int denominator = keyboard.nextInt(); double fraction; try { if ( denominator == 0 ) throw new Exception( “Exception! You can’t divide by zero”); fraction = (double) numerator / denominator ; System.out.println( fraction ); } catch ( Exception e ) { System.out.println( e.getMessage() ); System.out.println( “Go learn about division!” ); } System.out.println( “End of program” ); 7 catch block

try – throw - catch try { code to try possibly throw an exception of Exception_type and jump into the catch block if no exception is thrown, execute more code ignore catch block and continue program after it } catch ( Exception_type e ){ do stuff in this special case } 8

Other exception Classes FileNotFoundException 9

Streams Objects Deliver data from program to a destination – the screen or a file (output) Take data from a source – the keyboard or a file – and deliver it to the program (input) 10

Streams Input and output streams of data Objects of class Scanner are input streams The object System.out is an output stream 11

File versus Stream File – Permanent storage for an application (everything we’ve done before ‘disappears’) – Data available externally to the program (on disk, on memory stick, in the cloud) Stream – Representation of the file in the program

Writing to a file 1.Specify a filename 2.Attempt to open the file  associate file with a stream (fails if the file does not exist) 3.Write to the stream (file) 4.Close the stream (file) 13

Writing to a file java.io.PrintWriter; PrintWriter outStream = new PrintWriter( String fName );  starts with an empty file (overwrite possible) outStream.print(); outStream.println(); outStream.printf();  buffering outStream.close();  close your streams!!! 14

Writing to a file 1.get the filename 2.open the file 3.write to the stream 4.close the stream String fName = "readme.txt"; import java.io.PrintWriter; PrintWriter oStr; oStr = new PrintWriter(fName); oStr.println("hello"); oStr.printf(“this is %s“, fName); oStr.close();

import java.io.PrintWriter; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( fName ); oStream.println( "hello world!" ); oStream.close(); }

import java.io.PrintWriter; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( fName ); oStream.println( "hello world!" ); oStream.close(); }

import java.io.PrintWriter; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( fName ); oStream.println( "hello world!" ); oStream.close(); }

import java.io.PrintWriter; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( fName ); oStream.println( "hello world!" ); oStream.close(); } What if you run it multiple times? PrintWriter opens or creates a file … cursor is at the first position in the file (OVERWRITES any prior content)

import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( new FileOutputStream( fName, true ) ); oStream.println( "hello world!" ); oStream.close(); } This is how files are opened with the ability to APPEND the contents. Only changed the way the stream is created

import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( new FileOutputStream( fName, true ) ); oStream.println( "hello world!“ ); oStream.close(); }

import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( new FileOutputStream( fName, true ) ); oStream.println( "hello world!“ ); oStream.close(); } What if you try to append a file that doesn’t exist? The file will be created

import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( new FileOutputStream( fName, true ) ); oStream.println( "hello world!“ ); oStream.close(); } What if you run it multiple times?

import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( new FileOutputStream( fName, true ) ); oStream.println( "hello world!" ); oStream.close(); } What if you run it multiple times? You will get a file with 3 X N lines, where N is the number of times you run the program

Reading from a file 1.get the filename 2.open the file 3.read from the stream 4.close the stream String fName = "readme.txt"; import java.io.File; import java.util.Scanner; Scanner iStream; iStream = new Scanner( new File(fName)); i = iStream.nextInt(); iStream.close();

import java.io.FileNotFoundException; import java.io.File; import java.util.Scanner; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt”; String line; Scanner iStream; iStream = new Scanner( new File( fName ) ); line = iStream.nextLine(); System.out.println( line ); iStream.close(); }

import java.io.FileNotFoundException; import java.io.File; import java.util.Scanner; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt”; String line; Scanner iStream; iStream = new Scanner( new File( fName ) ); line = iStream.nextLine(); System.out.println( line ); iStream.close(); }

import java.io.FileNotFoundException; import java.io.File; import java.util.Scanner; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt”; String line; Scanner iStream; iStream = new Scanner( new File( fName ) ); line = iStream.nextLine(); System.out.println( line ); iStream.close(); } What if the file doesn’t exist? Exception in thread "main" java.io.FileNotFoundException: somefile.txt (The system cannot find the file specified)

import java.io.FileNotFoundException; import java.io.File; import java.util.Scanner; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt”; String line; Scanner iStream; iStream = new Scanner( new File( fName ) ); line = iStream.nextLine(); System.out.println( line ); iStream.close(); } What if the file is empty? Exception in thread "main" java.util.NoSuchElementException: No line found 

import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Scanner; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; String line; int lineNumber = 0; Scanner iStream = new Scanner( new File( fName ) ); while( iStream.hasNextLine() ) { line = iStream.nextLine(); lineNumber++; System.out.printf( "%d: %s\n", lineNumber, line ); } iStream.close(); }