Chapter 16 – Files and Streams. Announcements Only responsible for 16.1,16.3 Only responsible for 16.1,16.3 Other sections “encouraged” Other sections.

Slides:



Advertisements
Similar presentations
Lecture 15: I/O and Parsing
Advertisements

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 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.
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 
Lecture 7 File I/O (and a little bit about exceptions)‏
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.
1 File Input. 2 Reading Files We have already seen that writing files is (can be?) similar to using System.out Not surprisingly, reading from files is.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
Chapter 16 – 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.
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.
Two Ways to Store Data in a File Text format Binary format.
Chapter 16 Streams. Chapter Goals To be able to read and write text files To become familiar with the concepts of text and binary formats To learn about.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
Input / Output Chapter 13.  We use files all the time  Programs are files  Documents are files  We want them to be “permanent”  To last beyond execution.
Chapter 15 – Files I/O basics File I/O Classes File I/O Basic Operations Text File Output PrintWriter import Statement with a * Text File Input Scanner,
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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Working with files. RHS – SOC 2 Motivation All our programs so far have only worked with data stored in primary storage (RAM) Data is lost when program.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
Fall 2006Adapted from Java Concepts Companion Slides1 Files and Streams Advanced Programming ICOM 4015 Lecture 16 Reading: Java Concepts Chapter 16.
I/O in Java Dennis Burford
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.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
CSE 501N Fall ‘09 18: Files and Streams 06 November 2009 Nick Leidenfrost.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
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.
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
Week 12 – Text Files Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
 Learn about computer files  Use the Path class  Learn about  Streams  Buffers  file organization  Use Java’s IO classes to write to and read from.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
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
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
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.
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
For Friday Finish reading chapter 9 WebCT quiz 17.
OO Design and Programming II I/O: Reading and Writing
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.
Lesson 8: More File I/O February 5, 2008
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.
I/O Basics.
CSS161: Fundamentals of Computing
Working with files.
Working with files.
Streams and File I/O Chapter 14.
CSS 161: Fundamentals of Computing
File I/O ICS 111: Introduction to Computer Science I
18 File i/o, Parsing.
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
File Input and Output.
Presentation transcript:

Chapter 16 – Files and Streams

Announcements Only responsible for 16.1,16.3 Only responsible for 16.1,16.3 Other sections “encouraged” Other sections “encouraged” Responsible for online supplements for Exceptions and File I/O (see syllabus) Responsible for online supplements for Exceptions and File I/O (see syllabus)

Chapter 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 and binary formats To become familiar with the concepts of text and binary formats To learn about encryption To learn about encryption To understand when to use sequential and random file access To understand when to use sequential and random file access

MEM CPU HDD keyboard monitor terminal console standard input stream standard output stream Streams What does information travel across?

MEM CPU HDD keyboard monitor terminal console standard input stream standard output stream file input stream LOAD READ file output stream SAVE WRITE Streams files What does information travel across?

16.1 Reading and Writing Text Files Text files – files containing plain text Text files – files containing plain text Created with editors such as notepad, etc. Created with editors such as notepad, etc. Simplest way to learn it so extend our use of Scanner Simplest way to learn it so extend our use of Scanner Associate with files instead of System.in Associate with files instead of System.in All input classes, except Scanner, are in java.io All input classes, except Scanner, are in java.io import java.io.*; import java.io.*;

Review: Scanner Two ways to use scanner  two constructors Two ways to use scanner  two constructors First constructors takes an object of type java.io.InputStream – stores information about the connection between an input device and the computer or program First constructors takes an object of type java.io.InputStream – stores information about the connection between an input device and the computer or program Example: System.in Example: System.in Recall – only associate one instance of Scanner with System.in in your program Recall – only associate one instance of Scanner with System.in in your program

Review: Numerical Input First way: First way: Use nextInt() Use nextInt() int number = scanner.nextInt(); Second way: Second way: Use nextLine(), Integer.parseInt() Use nextLine(), Integer.parseInt() String input = scanner.nextLine(); int number = Integer.parseInt(input);

What’s the difference? Exceptions Exceptions nextInt() throws InputMismatchException nextInt() throws InputMismatchException parseInt() throws NumberFormatException parseInt() throws NumberFormatException Optimal use Optimal use nextInt() when multiple information on one line nextInt() when multiple information on one line nextLine() + parseInt() when one number per line nextLine() + parseInt() when one number per line

Reading To read from a disk file, construct a FileReader To read from a disk file, construct a FileReader Then, use the FileReader to construct a Scanner object Then, use the FileReader to construct a Scanner object FileReader reader = new FileReader("input.txt"); Scanner in = new Scanner(reader);

Alternative Use File instead of FileReader Use File instead of FileReader Has an exists( ) method we can call to avoid FileNotFoundException Has an exists( ) method we can call to avoid FileNotFoundException File file = new File ("input.txt"); Scanner in; if(file.exists()){ in = new Scanner(file); } else { //ask for another file }

What does this do? Allows us to use methods we already know Allows us to use methods we already know next, nextLine, nextInt, etc. next, nextLine, nextInt, etc. Reads the information from the file instead of console Reads the information from the file instead of console

File Class java.io.File java.io.File associated with actual file on hard drive associated with actual file on hard drive used to check file's status used to check file's status Constructors Constructors File( ), File(, ) File( ), File(, ) Predicate Methods Predicate Methods exists() exists() canRead(), canWrite() canRead(), canWrite() isFile(), isDirectory() isFile(), isDirectory()

Writing To File We will use a PrintWriter object to write to a file We will use a PrintWriter object to write to a file What if file already exists?  Empty file (delete whatever is there) What if file already exists?  Empty file (delete whatever is there) Doesn’t exist?  Create empty file with that name Doesn’t exist?  Create empty file with that name How do we use a PrintWriter object? How do we use a PrintWriter object? Have we already seen one? Almost. Have we already seen one? Almost.

PrintWriter The out field of System is a PrintStream object associated with the console. PrintWriter is a similar class optimized for writing characters. The out field of System is a PrintStream object associated with the console. PrintWriter is a similar class optimized for writing characters. We will associate our PrintWriter with a file now We will associate our PrintWriter with a file now Can use either a filename or File object Can use either a filename or File object PrintWriter fileOut = new PrintWriter("output.txt"); fileOut.println(29.95); fileOut.println(new Rectangle(5, 10, 15, 25)); fileOut.println("Hello, World!"); This will print the exact same information as with System.out (except to a file “output.txt”)! This will print the exact same information as with System.out (except to a file “output.txt”)!

Closing File Only difference is that we have to close the file stream when we are done writing Only difference is that we have to close the file stream when we are done writing If we do not, some output may not get written If we do not, some output may not get written At the end of output, call close() At the end of output, call close()fileOut.close();

Why? Short answer Short answer When you call print( ) and/or println( ), the output is actually written to buffer. When you close or flush the output, the buffer is written to the file When you call print( ) and/or println( ), the output is actually written to buffer. When you close or flush the output, the buffer is written to the file The slowest part of the computer is hard drive operations – much more efficient to write once instead of writing repeated times The slowest part of the computer is hard drive operations – much more efficient to write once instead of writing repeated times

File name When determining a file name, default is to place in the same directory as your.class files When determining a file name, default is to place in the same directory as your.class files If we want to define other place, use absolute path (e.g. C:\My Documents) If we want to define other place, use absolute path (e.g. C:\My Documents) in = new FileReader(“C:\\homework\\input.dat”);

Getting it all to work Remember: Remember: Have to import from java.io Have to import from java.io I/O requires us to catch checked exceptions I/O requires us to catch checked exceptions java.io.IOException java.io.IOException How long do we read from the file? Until the end. (duh) Use the hasNext( ), hasNextLine( ) and hasNextInt( ) predicate methods from Scanner. Otherwise you risk creating a NoSuchElementException

Java Input Review CONSOLE: Scanner stdin = new Scanner( System.in ); FILE: Scanner inFile = new Scanner( new File ( srcFileName ) );

import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class LineNumberer { public static void main(String[] args){ Scanner console = new Scanner(System.in); System.out.print(“Enter input file: "); String inFile = console.next(); System.out.print(“Enter output file: "); String outFile = console.next();

try{ File reader = new File(inFile); Scanner in = new Scanner(reader); PrintWriter out = new PrintWriter(outputFileName); int lineNumber = 1; while (in.hasNextLine()){ String line = in.nextLine(); out.println("/* " + lineNumber + " */ " + line); lineNumber++;}out.close(); } catch (IOException exception){ System.out.println("Error processing file: " + exception.getMessage()); System.out.println("Error processing file: " + exception.getMessage());} }}

Common Error You can run into problems using nextLine( ) in conjunction with nextInt( ) from the Scanner class. You can run into problems using nextLine( ) in conjunction with nextInt( ) from the Scanner class. In order to read this file In order to read this file You typed this code, but got this output You typed this code, but got this output What went wrong? What went wrong? 77hello int I = input.nextInt(); String s = input.nextLine(); System.out.println(i+”,”+s); 77,

Buffering gone bad To Java, the file is a long buffer of characters To Java, the file is a long buffer of characters nextInt removes the characters corresponding to a number, and that’s all. nextInt removes the characters corresponding to a number, and that’s all. nextLine looks for the next newline character (‘\n’), and returns everything before the first one it finds, even if that String is empty! nextLine looks for the next newline character (‘\n’), and returns everything before the first one it finds, even if that String is empty! 77\nhell… \nhello\n… hello\n… i = 77 s = “”

What to do? Avoid using nextInt( ) and nextLine( ) in combination Avoid using nextInt( ) and nextLine( ) in combination Always use nextLine( ) and convert to integers using Integer.parseInt( ) Always use nextLine( ) and convert to integers using Integer.parseInt( ) Use nextInt( ) in conjunction with next( ), which will skip over newlines to find the next non-whitespace string Use nextInt( ) in conjunction with next( ), which will skip over newlines to find the next non-whitespace string Check to see if Strings from nextLine( ) have length 0, and if so, call it again. Check to see if Strings from nextLine( ) have length 0, and if so, call it again.

16.3 An Encryption Program Demonstration: Use encryption to show file techniques Demonstration: Use encryption to show file techniques File encryption File encryption To scramble a file so that it is readable only to those who know the encryption method and secret keyword To scramble a file so that it is readable only to those who know the encryption method and secret keyword (Big area of CS in terms of commercial applications – biometrics, e-commerce, etc.) (Big area of CS in terms of commercial applications – biometrics, e-commerce, etc.)

Caesar Cipher Encryption key – the function to change the value Encryption key – the function to change the value Simple key – shift each letter over by 1 to 25 characters Simple key – shift each letter over by 1 to 25 characters If key = 3, A  D B  E etc. If key = 3, A  D B  E etc. Decrypt = reverse the encryption Decrypt = reverse the encryption Here we just subtract the key value Here we just subtract the key value

Caesar Cipher for alphabetic characters public void encrypt (Scanner in, PrintWriter out, int key) { while (in.hasNextLine()) { String line = in.nextLine(); String outLine = “”; for (int i=0; i<line.length; i++) { char c = line.charAt(i); if (c >= ‘a’ && c = ‘a’ && c <= ‘z’) c = (char)((c – ‘a’ + key) % 26 + ‘a’); else if (c >= ‘A’ && c = ‘A’ && c <= ‘Z’) c = (char)((c – ‘A’ + key) % 26 + ‘A’); outLine += c; }out.println(outLine);}} "Meet me at the secret place." key=5 => "Rjjy rj fy ymj xjhwjy uqfhj."

Modifications of Output Two constraints so far: Two constraints so far: Files are overwritten Files are overwritten Output is buffered and not written immediately Output is buffered and not written immediately We have options to get around this if we need to We have options to get around this if we need to More on that after this… More on that after this…

Tokenizing Often several text values are in a single line in a file to be compact Often several text values are in a single line in a file to be compact “ ” Line must be broken into parts (i.e. tokens) Line must be broken into parts (i.e. tokens)“25”“38”“36” Tokens then can be parsed as needed Tokens then can be parsed as needed “25” can be turned into the integer 25

Why Inputting each value on a new line makes the file very long Inputting each value on a new line makes the file very long May want a file of customer info – name, age, phone number all on one line May want a file of customer info – name, age, phone number all on one line File usually separate each piece of info with a delimiter – any special character designating a new piece of data (space in previous example) File usually separate each piece of info with a delimiter – any special character designating a new piece of data (space in previous example)

Tokenizing in Java Use a method of the String class called split Use a method of the String class called split Parameters: delimiting rules Parameters: delimiting rules Returns: An array of tokens Returns: An array of tokens We need to determine what delimiters are needed for each line. We need to determine what delimiters are needed for each line. Put them in a string that looks like this: Put them in a string that looks like this: “[ ]+” “[ ]+” “[,]+” “[,]+” “[ \n\t]+” “[ \n\t]+”

String Tokenizing in Java Scanner stdin = new Scanner(System.in); System.out.print("Enter a line with comma- separated integers(no space): " ); String input = stdin.nextLine(); String[] st = input.split(“[,]+”); for ( int i=0; i<st.length; i++ ) { int n = Integer.parseInt(st[i]); int n = Integer.parseInt(st[i]); System.out.println(n); System.out.println(n);}

What if I want to read this file? Class 1:8:10:7:6:5 Class 2:4:4:5:10:8:8:8 Class 3:6:7:9:10:7:5 Class 4:9:9:8:7:8 Class 5:9:10:9:3 Write a program to print out the average of the scores for each class

File gradeFile = new File(“scores.txt”); if(gradeFile.exists()) { Scanner inFile = new Scanner(gradeFile); while( inFile.hasNextLine() ) { String line = inFile.nextLine(); String[] st = line.split(“[:]+"); System.out.print(st[0] + “’s”); double sum = 0; for (int n=1; n<st.length; n++) sum += Integer.parseInt(st[n]); System.our.println(" average is "+ sum/(st.length-1)); }inFile.close();}

Modifications of Output Two constraints so far: Two constraints so far: Files are overwritten Files are overwritten Output is buffered and not written immediately Output is buffered and not written immediately But what if we want more control? But what if we want more control?

File Class java.io.FileWriter java.io.FileWriter Associated with File object Associated with File object Connects an output stream to write bytes of info Connects an output stream to write bytes of info Constructors Constructors FileWriter(, ); FileWriter(, ); true to append data, false to overwrite all of file true to append data, false to overwrite all of file This will overwrite an existing file This will overwrite an existing file To avoid, create File object and see if exists() is true To avoid, create File object and see if exists() is true

Java File Output PrintWriter PrintWriter composed from several objects composed from several objects PrintWriter out = new PrintWriter( new FileWriter( dstFileName, false ), true ); throws FileNotFoundException throws FileNotFoundException Methods Methods print(), println() : buffers data to write print(), println() : buffers data to write flush() : sends buffered output to destination flush() : sends buffered output to destination close() : flushes and closes stream close() : flushes and closes stream false: overwrite true: appends false: overwrite true: appends true: autoflush false: no autoflush true: autoflush false: no autoflush

Java File Output // Append to an existing file PrintWriter outFile1 = new PrintWriter( new FileWriter(dstFileName,true),false); // Autoflush on println PrintWriter outFile2 = new PrintWriter( new FileWriter(dstFileName,false),true); outFile1.println( “appended w/out flush” ); outFile2.println( “overwrite with flush” );

to flush or not to flush Advantage to flush: Advantage to flush: Safer – guaranteed that all of our data will write to the file Safer – guaranteed that all of our data will write to the file Disadvantage Disadvantage Less efficient – writing to file takes up time, more efficient to flush once (on close) Less efficient – writing to file takes up time, more efficient to flush once (on close) Can call flush( ) on a PrintWriter object created with just a filename at any time to force the buffer to disk Can call flush( ) on a PrintWriter object created with just a filename at any time to force the buffer to disk

Other Ways to Read/Write Files Binary files (InputStream/OutputStream) Binary files (InputStream/OutputStream) Storing readable text is rather inefficient (2 bytes/character, but we tend to use less than 100 letters) Storing readable text is rather inefficient (2 bytes/character, but we tend to use less than 100 letters) Images, music, class files use 0’s and 1’s directly Images, music, class files use 0’s and 1’s directly Can read and write these files, but we typically must work byte by byte Can read and write these files, but we typically must work byte by byte Random access (RandomAccessFile) Random access (RandomAccessFile) What if we don’t want to read from the beginning to the end? What if we don’t want to read from the beginning to the end? We have a “cursor” in the file, and can “seek” around to different points in any order, overwriting what was there before We have a “cursor” in the file, and can “seek” around to different points in any order, overwriting what was there before