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.

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.
COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Chapter 91 Streams and File I/O Chapter 9. 2 Announcements Project 5 due last night Project 6 assigned Exam 2 –Wed., March 21, 7:00 – 8:00 pm, LILY 1105.
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.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
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.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Chapter 20 – Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
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.
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.
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.
Stream: an object that either delivers data to its destination (screen, file, etc.) or that takes data from a source (keyboard, file, etc.) –it acts as.
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 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.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
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
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.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
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.
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.
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.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 (3 weeks)
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
OO Design and Programming II I/O: Reading and Writing
File Input / Output.
Streams & File Input/Output (I/O)
Lesson 8: More File I/O February 5, 2008
Streams and File I/O.
I/O Basics.
CSS161: Fundamentals of Computing
Streams and File I/O Chapter 9 Chapter 9.
Working with files.
Streams and File I/O Chapter 14.
CSS 161: Fundamentals of Computing
18 File i/o, Parsing.
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
File Input and Output.
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:

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 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 To be able to read and write objects using serialization To be able to read and write objects using serialization

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?

Reading and Writing Text Files Text files – files containing simple text Text files – files containing simple text Created with editors such as notepad, html, etc. Created with editors such as notepad, html, 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 We've seen Scanner before We've seen Scanner before The constructor takes an object of type java.io.InputStream – stores information about the connection between an input device and the computer or program The constructor 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 Otherwise, get bugs Otherwise, get bugs

Numerical Input 2 ways (we’ve learned one, seen the other) 2 ways (we’ve learned one, seen the other) Use int as example, similar for double Use int as example, similar for double 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);

Numerical Input Exceptions Exceptions nextInt() throws InputMismatchException nextInt() throws InputMismatchException parseInt() throws NumberFormatException parseInt() throws NumberFormatException Optimal use Optimal use nextInt() when there is multiple information on one line nextInt() when there is multiple information on one line nextLine() + parseInt() when one number per line nextLine() + parseInt() when one number per line

Reading Files The same applies for both console input and file input The same applies for both console input and file input We can use a different version of a Scanner that takes a File instead of System.in We can use a different version of a Scanner that takes a File instead of System.in Everything works the same! Everything works the same!

Reading Files 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 rdr = newFileReader("input.txt"); Scanner fin = new Scanner(rdr);

Reading Files You can use File instead of FileReader You can 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 fin; if(file.exists()){ fin = new Scanner(file); } else { //ask for another file }

Reading Files Once we have a Scanner, we can use methods we already know: Once we have a Scanner, we can 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 an actual file on hard drive associated with an actual file on hard drive used to check file's status used to check file's status Constructors Constructors File( ) File( ) File(, ) File(, ) Methods Methods exists() exists() canRead(), canWrite() canRead(), canWrite() isFile(), isDirectory() isFile(), isDirectory()

File Class java.io.FileReader java.io.FileReader Associated with File object Associated with File object Translates data bytes from File object into a stream of characters (much like InputStream vs. InputStreamReader) Translates data bytes from File object into a stream of characters (much like InputStream vs. InputStreamReader) Constructors Constructors FileReader( ); FileReader( ); Methods Methods read(), readLine() read(), readLine() close() close()

Writing to a 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 What if file already exists?  Empty file 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? Have we already seen one?

Writing to a File The out field of the System class is a PrintWriter object associated with the console The out field of the System class is a PrintWriter object associated with the console We will associate our PrintWriter with a file now We will associate our PrintWriter with a file now PrintWriter fout = new PrintWriter("output.txt"); fout.println(29.95); fout.println(new Rectangle(5, 10, 15, 25)); fout.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 a File Only main difference is that we have to close the file stream when we are done writing Only main difference is that we have to close the file stream when we are done writing If we do not, not all output will written If we do not, not all output will written At the end of output, call close() At the end of output, call close()fout.close();

Closing a File Why? Why? When you call print() and/or println(), the output is actually written to a 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 a 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 Locations When determining a file name, the default is to place in the same directory as your.class files When determining a file name, the default is to place in the same directory as your.class files If we want to define other place, use an absolute path (e.g. c:\My Documents) If we want to define other place, use an absolute path (e.g. c:\My Documents) in = new FileReader(“c:\\homework\\input.dat”); Why \\ ? Why \\ ?

Sample Program Two things to notice: Two things to notice: 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

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

Java Output Review CONSOLE: CONSOLE: System.out.print("To the screen"); FILE: FILE: PrintWriter fout = new PrintWriter(new File("output.txt"); fout.print("To a file");

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("Input file: "); String inFile = console.next(); System.out.print("Output file: "); String outFile = console.next(); try{ FileReader reader = new FileReader(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); System.out.println("Error processing file: " + exception);}}}

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, 128-bit encryption breaking, etc.) (Big area of CS in terms of commercial applications – biometrics, 128-bit encryption breaking, etc.)

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 We have options to get around this

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 ); requires throws FileNotFoundException, which is a sub class of IOException requires throws FileNotFoundException, which is a sub class of IOException 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

Java File Output // With append to an existing file PrintWriter outFile1 = new PrintWriter( new FileWriter(dstFileName,true),false); // With 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)

Caeser 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. Decryption = reversing the encryption Decryption = reversing the encryption Here we just subtract the key value Here we just subtract the key value

Binary File Encryption int next = in.read(); if (next == -1) done = true; else { byte b = (byte) next; //call the method to encrypt the byte byte c = encrypt(b); out.write(c);}

Object Streams Last example read BankAccount field individually Last example read BankAccount field individually Easier way to deal with whole object Easier way to deal with whole object ObjectOutputStream class can save a entire objects to disk ObjectOutputStream class can save a entire objects to disk ObjectOutputStream class can read objects back in from disk ObjectOutputStream class can read objects back in from disk Objects are saved in binary format; hence, you use streams and not writers Objects are saved in binary format; hence, you use streams and not writers

Write out an object The object output stream saves all instance variables The object output stream saves all instance variables BankAccount b =...; ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("bank.dat")); out.writeObject(b);

Read in an object readObject returns an Object reference readObject returns an Object reference Need to remember the types of the objects that you saved and use a cast Need to remember the types of the objects that you saved and use a cast ObjectInputStream in = new ObjectInputStream( new FileInputStream("bank.dat")); BankAccount b = (BankAccount) in.readObject();

Exceptions readObject method can throw a ClassNotFoundException readObject method can throw a ClassNotFoundException It is a checked exception It is a checked exception You must catch or declare it You must catch or declare it

Writing an Array Usually want to write out a collection of objects: Usually want to write out a collection of objects: BankAccount[] arr = new BankAccount[size]; // Now add size BankAccount objects into arr out.writeObject(arr);

Reading an Array To read a set of objects into an array To read a set of objects into an array BankAccount[] ary = (BankAccount[]) in.readObject();

Object Streams Very powerful features Very powerful features Especially considering how little we have to do Especially considering how little we have to do The BankAccount class as is actually will not work with the stream The BankAccount class as is actually will not work with the stream Must implement Serializable interface in order for the formatting to work Must implement Serializable interface in order for the formatting to work

Object Streams class BankAccount implements Serializable {... } IMPORTANT: Serializable interface has no methods. IMPORTANT: Serializable interface has no methods. No effort required No effort required

Serialization Serialization: process of saving objects to a stream Serialization: process of saving objects to a stream Each object is assigned a serial number on the stream Each object is assigned a serial number on the stream If the same object is saved twice, only serial number is written out the second time If the same object is saved twice, only serial number is written out the second time When reading, duplicate serial numbers are restored as references to the same object When reading, duplicate serial numbers are restored as references to the same object

Serialization Why isn’t everything serializable? Why isn’t everything serializable? Security reasons – may not want contents of objects printed out to disk, then anyone can print out internal structure and analyze it Security reasons – may not want contents of objects printed out to disk, then anyone can print out internal structure and analyze it Example: Don’t want SSN ever being accessed Example: Don’t want SSN ever being accessed Could also have temporary variables that are useless once the program is done running Could also have temporary variables that are useless once the program is done running

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 “ ” The line must be broken into parts (i.e. tokens) The 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

Tokenizing 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 StringTokenizer object use a StringTokenizer object default delimiters are: space, tab, newline, return default delimiters are: space, tab, newline, return requires: import java.util.* requires: import java.util.* Constructors Constructors StringTokenizer(String line)//default dlms StringTokenizer(String line)//default dlms StringTokenizer(String ln, String dlms) StringTokenizer(String ln, String dlms) Methods Methods hasMoreTokens() hasMoreTokens() nextToken() nextToken() countTokens() countTokens()

StringTokenizing in Java Scanner stdin = new… System.out.print( "Enter a line with comma seperated integers(no space): " ); String input = stdin.nextLine(); StringTokenizer st; String delims = ","; st = new StringTokenizer( input, delims ); while ( st.hasMoreTokens() ) { int n = Integer.parseInt(st.nextToken()); int n = Integer.parseInt(st.nextToken()); System.out.println(n); System.out.println(n);}

File gradeFile = new File(“scores.txt”); if(gradeFile.exists()){ Scanner inFile = new Scanner(gradeFile); String line = inFile.nextLine(); while(line != null){ StringTokenizer st = new StringTokenizer(line, ":"); System.out.print(" Name: " + st.nextToken()); int num = 0; double sum = 0; while ( st.hasMoreTokens() ) {num++; sum += Integer.parseInt(st.nextToken()); } System.our.println(" average = "+ sum/num); line = inFile.nextLine();

}inFile.close();} If you call nextToken() and there are no more tokens, NoSuchElementException is thrown

Tokenizing Scanner tokenizes already… Scanner tokenizes already… Scanner in = new Scanner(…); while(in.hasNext()) { String str = in.next(); …}