Title: Chapter 16
1Chapter 16 Files and Streams
2Announcements
- Only responsible for 16.1,16.3
- Other sections encouraged
- Responsible for online supplements for Exceptions
and File I/O (see syllabus)
3Chapter Goals
- To be able to read and write text files
- To become familiar with the concepts of text and
binary formats - To learn about encryption
- To understand when to use sequential and random
file access
4keyboard
standard input stream
standardoutput stream
monitorterminalconsole
What does information travel across?
Streams
5keyboard
standard input stream
standardoutput stream
monitorterminalconsole
file input stream LOAD READ
What does information travel across?
file output stream SAVE WRITE
files
Streams
616.1 Reading and Writing Text Files
- Text files files containing plain text
- Created with editors such as notepad, etc.
- Simplest way to learn it so extend our use of
Scanner - Associate with files instead of System.in
- All input classes, except Scanner, are in java.io
- import java.io.
7Review Scanner
- 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 - Example System.in
- Recall only associate one instance of Scanner
with System.in in your program
8Review Numerical Input
- First way
- Use nextInt()
- int number scanner.nextInt()
- Second way
- Use nextLine(), Integer.parseInt()
- String input scanner.nextLine()
- int number Integer.parseInt(input)
9Whats the difference?
- Exceptions
- nextInt() throws InputMismatchException
- parseInt() throws NumberFormatException
- Optimal use
- nextInt() when multiple information on one line
- nextLine() parseInt() when one number per line
10Reading
- To read from a disk file, construct a FileReader
- Then, use the FileReader to construct a Scanner
object - FileReader reader new FileReader("input.txt")
- Scanner in new Scanner(reader)
11Alternative
- Use File instead of FileReader
- 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
12What does this do?
- Allows us to use methods we already know
- next, nextLine, nextInt, etc.
- Reads the information from the file instead of
console
13File Class
- java.io.File
- associated with actual file on hard drive
- used to check file's status
- Constructors
- File(ltfull pathgt), File(ltpathgt, ltfilenamegt)
- Predicate Methods
- exists()
- canRead(), canWrite()
- isFile(), isDirectory()
14Writing To File
- We will use a PrintWriter object to write to a
file - What if file already exists? ? Empty file (delete
whatever is there) - Doesnt exist? ? Create empty file with that name
- How do we use a PrintWriter object?
- Have we already seen one? Almost.
15PrintWriter
- 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
- 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)!
16Closing File
- 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
- At the end of output, call close()
- fileOut.close()
17Why?
- 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 - The slowest part of the computer is hard drive
operations much more efficient to write once
instead of writing repeated times
18File name
- 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) - in new FileReader(C\\homework\\input.dat)
19Getting it all to work
- Remember
- Have to import from java.io
- I/O requires us to catch checked exceptions
- 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 NoSuchElementExcepti
on
20Java Input Review
- CONSOLE
- Scanner stdin new Scanner( System.in )
- FILE
- Scanner inFile new
- Scanner( new File ( srcFileName ) )
21- 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()
22- 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()) -
-
-
23Common Error
- You can run into problems using nextLine( ) in
conjunction with nextInt( ) from the Scanner
class. - In order to read this file
- You typed this code, but got this output
- What went wrong?
77 hello
int I input.nextInt() String s
input.nextLine() System.out.println(i,s)
77,
24Buffering gone bad
- To Java, the file is a long buffer of characters
- nextInt removes the characters corresponding to a
number, and thats all. - nextLine looks for the next newline character
(\n), and returns everything before the first
one it finds, even if that String is empty!
7 7 \n h e l l
\n h e l l o \n
i 77
h e l l o \n
s
25What to do?
- Avoid using nextInt( ) and nextLine( ) in
combination - 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 - Check to see if Strings from nextLine( ) have
length 0, and if so, call it again.
2616.3 An Encryption Program
- Demonstration Use encryption to show file
techniques - File encryption
- 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.)
27Caesar Cipher
- Encryption key the function to change the value
- Simple key shift each letter over by 1 to 25
characters - If key 3, A ? D B ? E etc.
- Decrypt reverse the encryption
- Here we just subtract the key value
28Caesar Cipher for alphabetic characters
- public void encrypt (Scanner in, PrintWriter out,
int key) - while (in.hasNextLine())
- String line in.nextLine()
- String outLine
- for (int i0 iltline.length i)
- char c line.charAt(i)
- if (c gt a c lt z)
- c (char)((c a key) 26 a)
- else if (c gt A c lt Z)
- c (char)((c A key) 26 A)
- outLine c
-
- out.println(outLine)
-
"Meet me at the secret place." key5 gt "Rjjy rj
fy ymj xjhwjy uqfhj."
29Modifications of Output
- Two constraints so far
- Files are overwritten
- Output is buffered and not written immediately
- We have options to get around this if we need to
- More on that after this
30Tokenizing
- Often several text values are in a single line in
a file to be compact - 25 38 36 34 29 60 59
- Line must be broken into parts (i.e. tokens)
- 25
- 38
- 36
- Tokens then can be parsed as needed
- 25 can be turned into the integer 25
31Why
- 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 - File usually separate each piece of info with a
delimiter any special character designating a
new piece of data (space in previous example)
32Tokenizing in Java
- Use a method of the String class called split
- Parameters delimiting rules
- Returns An array of tokens
- We need to determine what delimiters are needed
for each line. - Put them in a string that looks like this
- ltdelimetersgt
- ,
- \n\t
33String 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 i0 iltst.length i )
-
- int n Integer.parseInt(sti)
- System.out.println(n)
34What if I want to read this file?
Class 1810765 Class 244510888 Class
36791075 Class 499878 Class 591093
Write a program to print out the average of the
scores for each class
35- 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(st0 s)
-
- double sum 0
- for (int n1 nltst.length n)
- sum Integer.parseInt(stn)
- System.our.println(" average is "
sum/(st.length-1)) -
- inFile.close()
-
36Modifications of Output
- Two constraints so far
- Files are overwritten
- Output is buffered and not written immediately
- But what if we want more control?
37File Class
- java.io.FileWriter
- Associated with File object
- Connects an output stream to write bytes of info
- Constructors
- FileWriter( ltFilenamegt, ltbooleangt )
- true to append data, false to overwrite all of
file - This will overwrite an existing file
- To avoid, create File object and see if exists()
is true
38Java File Output
- PrintWriter
- composed from several objects
- PrintWriter out new PrintWriter( new
FileWriter( dstFileName, false ), true ) - throws FileNotFoundException
- Methods
- print(), println() buffers data to write
- flush() sends buffered output to destination
- close() flushes and closes stream
false overwrite true appends
true autoflush false no autoflush
39Java 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 )
40to flush or not to flush
- Advantage to flush
- Safer guaranteed that all of our data will
write to the file - Disadvantage
- 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
41Other Ways to Read/Write Files
- Binary files (InputStream/OutputStream)
- Storing readable text is rather inefficient (2
bytes/character, but we tend to use less than 100
letters) - Images, music, class files use 0s and 1s
directly - Can read and write these files, but we typically
must work byte by byte - Random access (RandomAccessFile)
- What if we dont 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