chapter 16 – files and streams. announcements only responsible for 16.1,16.3 only responsible for...
TRANSCRIPT
Chapter 16 – Chapter 16 – Files and Files and StreamsStreams
AnnouncementsAnnouncements
Only responsible for 16.1,16.3Only responsible for 16.1,16.3 Other sections “encouraged”Other sections “encouraged”
Responsible for online supplements Responsible for online supplements for Exceptions and File I/O (see for Exceptions and File I/O (see syllabus)syllabus)
Chapter GoalsChapter Goals
To be able to read and write text To be able to read and write text files files
To become familiar with the To become familiar with the concepts of text and binary formats concepts of text and binary formats
To learn about encryption To learn about encryption To understand when to use To understand when to use
sequential and random file accesssequential and random file access
MEM
CPU
HDD
keyboard
monitorterminalconsole
standard input
stream
standardoutput stream
StreamsStreams
What does information What does information travel across?travel across?
MEM
CPU
HDD
keyboard
monitorterminalconsole
standard input
stream
standardoutput stream
fileinput strea
mLOADREAD file
output
stream
SAVEWRITE
StreamsStreams files
What does What does information travel information travel
across?across?
16.1 Reading and Writing 16.1 Reading and Writing Text FilesText Files
Text files – files containing plain textText 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 Simplest way to learn it so extend our use of use of ScannerScanner Associate with files instead of Associate with files instead of System.inSystem.in
All input classes, except Scanner, are All input classes, except Scanner, are in java.ioin java.io import java.io.*;import java.io.*;
Review: ScannerReview: Scanner
Two ways to use scanner Two ways to use scanner two two constructorsconstructors
First constructors takes an object of First constructors takes an object of type type java.io.InputStreamjava.io.InputStream – stores – stores information about the connection information about the connection between an input device and the between an input device and the computer or programcomputer or program Example: Example: System.inSystem.in
Recall – only associate one instance of Recall – only associate one instance of Scanner Scanner with with System.inSystem.in in your program in your program
Review: Numerical InputReview: Numerical Input First way:First way:
Use Use nextInt()nextInt()
int number = scanner.nextInt();int number = scanner.nextInt();
Second way:Second way: Use Use nextLine(), Integer.parseInt()nextLine(), Integer.parseInt()
String input = scanner.nextLine();String input = scanner.nextLine();
int number = Integer.parseInt(input);int number = Integer.parseInt(input);
What’s the difference?What’s the difference?
ExceptionsExceptions nextInt()nextInt() throws throws InputMismatchExceptionInputMismatchException parseInt()parseInt() throws throws NumberFormatExceptionNumberFormatException
Optimal useOptimal use nextInt()nextInt() when multiple information on when multiple information on
one lineone line nextLine() + parseInt()nextLine() + parseInt() when one when one
number per linenumber per line
ReadingReading
To read from a disk file, construct a To read from a disk file, construct a FileReaderFileReader
Then, use the Then, use the FileReaderFileReader to construct to construct a a ScannerScanner object object
FileReader reader = new FileReader("input.txt"); FileReader reader = new FileReader("input.txt");
Scanner in = new Scanner(reader); Scanner in = new Scanner(reader);
AlternativeAlternative
Use Use FileFile instead of instead of FileReader FileReader
Has an exists( ) method we can call to Has an exists( ) method we can call to avoid FileNotFoundExceptionavoid FileNotFoundException
File file = new File ("input.txt"); File file = new File ("input.txt");
Scanner in;Scanner in;
if(file.exists()){if(file.exists()){
in = new Scanner(file);in = new Scanner(file);
} else {} else {
//ask for another file//ask for another file
}}
What does this do?What does this do?
Allows us to use methods we already Allows us to use methods we already knowknow next, nextLine, nextIntnext, nextLine, nextInt, etc., etc.
Reads the information from the file Reads the information from the file instead of consoleinstead of console
File ClassFile Class java.io.Filejava.io.File
associated with actual file on hard driveassociated with actual file on hard drive used to check file's statusused to check file's status
ConstructorsConstructors File(<full path>)File(<full path>), , File(<path>, <filename>)File(<path>, <filename>)
Predicate MethodsPredicate Methods exists()exists() canRead()canRead(), , canWrite()canWrite() isFile()isFile(), , isDirectory()isDirectory()
Writing To FileWriting To File
We will use a We will use a PrintWriterPrintWriter object to object to write to a filewrite to a file What if file already exists? What if file already exists? Empty file Empty file
(delete whatever is there)(delete whatever is there) Doesn’t exist? Doesn’t exist? Create empty file with Create empty file with
that namethat name
How do we use a How do we use a PrintWriterPrintWriter object? object? Have we already seen one? Almost.Have we already seen one? Almost.
PrintWriterPrintWriter The The outout field of field of SystemSystem is a is a PrintStream PrintStream object object
associated with the console. associated with the console. PrintWriterPrintWriter is a is a similar class optimized for writing characters.similar class optimized for writing characters. We will associate our We will associate our PrintWriterPrintWriter with a file now with a file now Can use either a filename or File objectCan use either a filename or File object
PrintWriter fileOut = new PrintWriter("output.txt"); PrintWriter fileOut = new PrintWriter("output.txt"); fileOut.println(29.95);fileOut.println(29.95);fileOut.println(new Rectangle(5, 10, 15, 25));fileOut.println(new Rectangle(5, 10, 15, 25));
fileOut.println("Hello, World!");fileOut.println("Hello, World!"); This will print the exact same information as This will print the exact same information as
with System.out (except to a file “output.txt”)!with System.out (except to a file “output.txt”)!
Closing FileClosing File
Only difference is that we have to close Only difference is that we have to close the file stream when we are done writingthe file stream when we are done writing
If we do not, some output may not get If we do not, some output may not get writtenwritten
At the end of output, call close()At the end of output, call close()
fileOut.close(); fileOut.close();
Why?Why?
Short answerShort answer When you call print( ) and/or println( ), When you call print( ) and/or println( ),
the output is actually written to buffer. the output is actually written to buffer. When you close or flush the output, the When you close or flush the output, the buffer is written to the filebuffer is written to the file
The slowest part of the computer is The slowest part of the computer is hard drive operations – much more hard drive operations – much more efficient to write once instead of writing efficient to write once instead of writing repeated timesrepeated times
File nameFile name
When determining a file name, default is When determining a file name, default is to place in the same directory as to place in the same directory as your .class filesyour .class files
If we want to define other place, use If we want to define other place, use absolute path (e.g. C:\My Documents)absolute path (e.g. C:\My Documents)
in = new FileReader(“C:\\homework\\input.dat”);in = new FileReader(“C:\\homework\\input.dat”);
Getting it all to workGetting it all to work
Remember:Remember: Have to import from java.ioHave to import from java.io I/O requires us to catch checked exceptionsI/O requires us to catch checked exceptions
java.io.IOExceptionjava.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 ReviewJava Input ReviewCONSOLE:CONSOLE:
Scanner stdin = new Scanner( System.in );Scanner stdin = new Scanner( System.in );
FILE:FILE:
Scanner inFile = new Scanner inFile = new
Scanner( new File ( srcFileName ) );Scanner( new File ( srcFileName ) );
import java.io.FileReader; import java.io.FileReader; import java.io.IOException;import java.io.IOException;import java.io.PrintWriter; import java.io.PrintWriter; import java.util.Scanner; import java.util.Scanner;
public class LineNumberer {public class LineNumberer {public static void main(String[] args){public static void main(String[] args){
Scanner console = new Scanner(System.in);Scanner console = new Scanner(System.in);System.out.print(“Enter input file: ");System.out.print(“Enter input file: ");String inFile = console.next();String inFile = console.next();
System.out.print(“Enter output file: ");System.out.print(“Enter output file: ");
String outFile = console.next();String outFile = console.next();
try{try{
File reader = new File(inFile); File reader = new File(inFile);
Scanner in = new Scanner(reader); Scanner in = new Scanner(reader);
PrintWriter out = new PrintWriter(outputFileName);PrintWriter out = new PrintWriter(outputFileName);
int lineNumber = 1;int lineNumber = 1;
while (in.hasNextLine()){while (in.hasNextLine()){
String line = in.nextLine();String line = in.nextLine();
out.println("/* " + lineNumber + " */ " + line);out.println("/* " + lineNumber + " */ " + line);
lineNumber++;lineNumber++;
}}
out.close();out.close();
} catch (IOException exception){} catch (IOException exception){
System.out.println("Error processing file: " System.out.println("Error processing file: " + + exception.getMessage());exception.getMessage());
}}
}}
} }
Common ErrorCommon Error
You can run into problems using You can run into problems using nextLine( ) in conjunction with nextLine( ) in conjunction with nextInt( ) from the Scanner class.nextInt( ) from the Scanner class. In order to read this fileIn order to read this file You typed this code, but got this outputYou typed this code, but got this output
What went wrong?What went wrong?
7777
hellohello
int I = input.nextInt();int I = input.nextInt();
String s = input.nextLine();String s = input.nextLine();
System.out.println(i+”,”+s);System.out.println(i+”,”+s);
77,77,
Buffering gone badBuffering gone bad To Java, the file is a To Java, the file is a
long buffer of long buffer of characterscharacters
nextInt removes the nextInt removes the characters characters corresponding to a corresponding to a number, and that’s all.number, and that’s all.
nextLine looks for the nextLine looks for the next newline character next newline character (‘\n’), and returns (‘\n’), and returns everything before the everything before the first one it finds, even if first one it finds, even if that String is empty!that String is empty!
77 77 \n\n hh ee ll ll ……
\n\n hh ee ll ll oo \n\n ……
hh ee ll ll oo \n\n ……
i = 77i = 77
s = “”s = “”
What to do?What to do?
Avoid using nextInt( ) and nextLine( ) Avoid using nextInt( ) and nextLine( ) in combinationin combination Always use nextLine( ) and convert to Always use nextLine( ) and convert to
integers using Integer.parseInt( )integers using Integer.parseInt( ) Use nextInt( ) in conjunction with next( ), Use nextInt( ) in conjunction with next( ),
which will skip over newlines to find the which will skip over newlines to find the next non-whitespace stringnext non-whitespace string
Check to see if Strings from nextLine( Check to see if Strings from nextLine( ) have length 0, and if so, call it again.) have length 0, and if so, call it again.
16.3 An Encryption 16.3 An Encryption ProgramProgram
Demonstration: Use encryption to Demonstration: Use encryption to show file techniquesshow file techniques
File encryption File encryption To scramble a file so that it is readable To scramble a file so that it is readable
only to those who know the encryption only to those who know the encryption method and secret keywordmethod and secret keyword
(Big area of CS in terms of commercial (Big area of CS in terms of commercial applications – biometrics, e-commerce, applications – biometrics, e-commerce, etc.)etc.)
Caesar CipherCaesar Cipher Encryption key – the function to change Encryption key – the function to change
the valuethe value Simple key – shift each letter over by 1 to Simple key – shift each letter over by 1 to
25 characters25 characters If key = 3, A If key = 3, A D B D B E etc. E etc.
Decrypt = reverse the encryptionDecrypt = reverse the encryption Here we just subtract the key valueHere we just subtract the key value
Caesar Cipher for alphabetic Caesar Cipher for alphabetic characterscharacters
public void encrypt (Scanner in, PrintWriter out, int key) {public void encrypt (Scanner in, PrintWriter out, int key) {while (in.hasNextLine()) {while (in.hasNextLine()) {
String line = in.nextLine();String line = in.nextLine();String outLine = “”;String outLine = “”;for (int i=0; i<line.length; i++) {for (int i=0; i<line.length; i++) {
char c = line.charAt(i);char c = line.charAt(i);if (c >= ‘a’ && c <= ‘z’)if (c >= ‘a’ && c <= ‘z’)
c = (char)((c – ‘a’ + key) % 26 + ‘a’);c = (char)((c – ‘a’ + key) % 26 + ‘a’);else if (c >= ‘A’ && c <= ‘Z’)else if (c >= ‘A’ && c <= ‘Z’)
c = (char)((c – ‘A’ + key) % 26 + ‘A’);c = (char)((c – ‘A’ + key) % 26 + ‘A’);outLine += c;outLine += c;
}}out.println(outLine);out.println(outLine);
}}}}
"Meet me at the secret place." key=5 => "Rjjy rj fy ymj xjhwjy uqfhj."
Modifications of OutputModifications of Output
Two constraints so far:Two constraints so far: Files are overwrittenFiles are overwritten Output is buffered and not written Output is buffered and not written
immediatelyimmediately
We have options to get around this if We have options to get around this if we need towe need to
More on that after this…More on that after this…
Tokenizing Tokenizing Often several text values are in a single Often several text values are in a single
line in a file to be compactline in a file to be compact““25 38 36 34 29 60 59”25 38 36 34 29 60 59”
Line must be broken into parts (i.e. Line must be broken into parts (i.e. tokenstokens))
““25”25”
““38”38”
““36”36”
Tokens then can be parsed as neededTokens then can be parsed as needed““25”25” can be turned into the integercan be turned into the integer 2525
WhyWhy
Inputting each value on a new line Inputting each value on a new line makes the file very longmakes the file very long
May want a file of customer info – name, May want a file of customer info – name, age, phone number all on one lineage, phone number all on one line
File usually separate each piece of info File usually separate each piece of info with a with a delimiterdelimiter – any special character – any special character designating a new piece of data (space designating a new piece of data (space in previous example)in previous example)
Tokenizing in JavaTokenizing in Java
Use a method of the String class called Use a method of the String class called splitsplit Parameters: delimiting rulesParameters: delimiting rules Returns: An array of tokensReturns: An array of tokens
We need to determine what delimiters We need to determine what delimiters are needed for each line.are needed for each line.
Put them in a string that looks like this:Put them in a string that looks like this: ““[<delimeters>]+”[<delimeters>]+” ““[,]+”[,]+” ““[ \n\t]+” [ \n\t]+”
String Tokenizing in JavaString Tokenizing in JavaScanner stdin = new Scanner(System.in);Scanner stdin = new Scanner(System.in);
System.out.print("Enter a line with comma- System.out.print("Enter a line with comma- separated integers(no space): " );separated integers(no space): " );
String input = stdin.nextLine();String input = stdin.nextLine();
String[] st = input.split(“[,]+”);String[] st = input.split(“[,]+”);
for ( int i=0; i<st.length; i++ ) 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?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 Write a program to print out the average of the scores for each classof the scores for each class
File gradeFile = new File(“scores.txt”);File gradeFile = new File(“scores.txt”);
if(gradeFile.exists()) {if(gradeFile.exists()) {
Scanner inFile = new Scanner(gradeFile);Scanner inFile = new Scanner(gradeFile);
while( inFile.hasNextLine() ) {while( inFile.hasNextLine() ) {
String line = inFile.nextLine();String line = inFile.nextLine();
String[] st = line.split(“[:]+");String[] st = line.split(“[:]+");
System.out.print(st[0] + “’s”);System.out.print(st[0] + “’s”);
double sum = 0;double sum = 0;
for (int n=1; n<st.length; n++) for (int n=1; n<st.length; n++)
sum += Integer.parseInt(st[n]);sum += Integer.parseInt(st[n]);
System.our.println(" average is "+ sum/(st.length-1));System.our.println(" average is "+ sum/(st.length-1));
}}
inFile.close();inFile.close();
}}
Modifications of OutputModifications of Output
Two constraints so far:Two constraints so far: Files are overwrittenFiles are overwritten Output is buffered and not written Output is buffered and not written
immediatelyimmediately
But what if we want more control?But what if we want more control?
File ClassFile Class java.io.FileWriterjava.io.FileWriter
Associated with Associated with FileFile object object Connects an output stream to write bytes of Connects an output stream to write bytes of
infoinfo
ConstructorsConstructors FileWriter( <Filename>, <boolean> );FileWriter( <Filename>, <boolean> );
true to append data, false to overwrite all of true to append data, false to overwrite all of filefile
This will overwrite an existing fileThis will overwrite an existing file To avoid, create File object and see if To avoid, create File object and see if exists()exists() is true is true
Java File OutputJava File Output PrintWriterPrintWriter
composed from several objectscomposed from several objectsPrintWriter out = PrintWriter out = new PrintWriter( new PrintWriter(
new FileWriter(new FileWriter( dstFileNamedstFileName,, false false )), , truetrue ); );
throws FileNotFoundExceptionthrows FileNotFoundException
MethodsMethods print()print(), , println()println(): : buffersbuffers data to write data to write flush()flush():: sends buffered output to sends buffered output to
destinationdestination close()close():: flushes and closes stream flushes and closes stream
false: overwrite
true: appends
false: overwrite
true: appends
true: autoflushfalse: no autoflush
true: autoflushfalse: no autoflush
Java File OutputJava File Output
// Append to an existing file// Append to an existing filePrintWriter outFile1 = PrintWriter outFile1 =
new PrintWriter(new PrintWriter( new FileWriter( new FileWriter(dstFileNamedstFileName,,true),falsetrue),false););
// Autoflush on println// Autoflush on printlnPrintWriter outFile2 =PrintWriter outFile2 =
new PrintWriter(new PrintWriter( new FileWriter(dstFileName,new FileWriter(dstFileName,falsefalse),),truetrue););
outFile1.println( “appended w/out flush” );outFile1.println( “appended w/out flush” );outFile2.println( “overwrite with flush” );outFile2.println( “overwrite with flush” );
to flush or not to flushto flush or not to flush
Advantage to flush:Advantage to flush: Safer – guaranteed that all of our data will write Safer – guaranteed that all of our data will write
to the fileto the file
DisadvantageDisadvantage Less efficient – writing to file takes up time, Less efficient – writing to file takes up time,
more efficient to flush once (on close)more efficient to flush once (on close)
Can call flush( ) on a PrintWriter object Can call flush( ) on a PrintWriter object created with just a filename at any time to created with just a filename at any time to force the buffer to diskforce the buffer to disk
Other Ways to Other Ways to Read/Write FilesRead/Write Files
Binary files (InputStream/OutputStream)Binary files (InputStream/OutputStream) Storing readable text is rather inefficient (2 Storing readable text is rather inefficient (2
bytes/character, but we tend to use less than 100 letters)bytes/character, but we tend to use less than 100 letters) Images, music, class files use 0’s and 1’s directlyImages, music, class files use 0’s and 1’s directly Can read and write these files, but we typically must work Can read and write these files, but we typically must work
byte by bytebyte by byte Random access (RandomAccessFile)Random access (RandomAccessFile)
What if we don’t want to read from the beginning to the What if we don’t want to read from the beginning to the end?end?
We have a “cursor” in the file, and can “seek” around to We have a “cursor” in the file, and can “seek” around to different points in any order, overwriting what was there different points in any order, overwriting what was there beforebefore