Lecture 20: Java File I/o
description
Transcript of Lecture 20: Java File I/o
![Page 1: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/1.jpg)
LECTURE 20: JAVA FILE I/O
CSC 213 – Large Scale Programming
![Page 2: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/2.jpg)
Today's Goals
Discuss reasons why files & file I/O important When & where used and what real value does
it offer? Show how to read & write text to files in
Java Classes & methods needed to perform these
actions How these methods move through file as they
work Limits of these actions & why we might want
more Discuss another approach: RandomAccessFiles Benefits of using this for reading & writing
data How this also has additional ways to access
data
![Page 3: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/3.jpg)
Image To Sharpen
I have a (fuzzy) 1024 x 768 picture to sharpen Only 786,432 numbers to type into photo
application After analysis, must click & update each
pixel
![Page 4: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/4.jpg)
Image To Sharpen
I have a (fuzzy) 1024 x 768 picture to sharpen Only 786,432 numbers to type into photo
application After analysis, must click & update each
pixel
![Page 5: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/5.jpg)
More Data Entry Positions
Testing improved jet designs for oeingB-ay Using program to simulate designs' lift &
drag 5 possible designs (each 150MB) to test
this iteration Once results available, will tweak & retest
designs Need room of touch typists for all this data
entry
![Page 6: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/6.jpg)
This Is (Semi-Real) Problem
Large hadron collider about to come on-line No black hole when smashing particles at
high speeds Creates 28.5 GB/min for nerds seeking
truth & beauty
![Page 7: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/7.jpg)
This Is (Semi-Real) Problem
Large hadron collider about to come on-line No black hole when smashing particles at
high speeds Creates 28.5 GB/min for nerds seeking
truth & beauty Hired trained monkeys to type data into
programs
![Page 8: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/8.jpg)
This Is (Semi-Real) Problem
Large hadron collider about to come on-line No black hole when smashing particles at
high speeds Creates 28.5 GB/min for nerds seeking
truth & beauty Hired trained monkeys to type data into
programs college students
![Page 9: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/9.jpg)
This Is (Semi-Real) Problem
Large hadron collider about to come on-line No black hole when smashing particles at
high speeds Creates 28.5 GB/min for nerds seeking
truth & beauty Hired trained monkeys to type data into
programs college students
![Page 10: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/10.jpg)
Yeah, Right
![Page 11: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/11.jpg)
Yeah, Right
Real world demands we use files for most I/O
Data files used to start and/or end most projects May contain: game levels, analysis results,
CCD pics Way to read & write files needed to be
useful
![Page 12: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/12.jpg)
Reading a Text File
Must first instantiate java.io.File object Pass String filename to the File
constructor Throws a (checked) exception if file does
not exist Another IOException possible for other odd
errors Once created, use File to create Scanner Reads file's data rather than typing into
keyboard At the same time, works like any other Scanner
![Page 13: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/13.jpg)
Reading a Text File
try {File readFile = new File("bob.dat");Scanner scan = new Scanner(readFile);while (scan.hasNext()) { String line = scan.nextLine(); System.out.println(line);}scan.close();
} catch (FileNotFoundException fnfe) {System.err.println("Make the file, moron!");
} catch (IOException ioe) {ioe.printStackTrace();
}
![Page 14: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/14.jpg)
Typical File I/O
Ordinarily we read files sequentiallyScanner scan ;// Instantiate a Scanner scan for the “file” belowchar c = ‘’;while (c != ‘s’) {
c = scan.nextChar(); }
scan
Are 10^15 Files Just a Peta-File?
![Page 15: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/15.jpg)
Typical File I/O
Ordinarily we read files sequentiallyScanner scan ;// Instantiate a Scanner scan for the “file” belowchar c = ‘’;while (c != ‘s’) {
c = scan.nextChar(); }
scan
Are 10^15 Files Just a Peta-File?
![Page 16: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/16.jpg)
Typical File I/O
Ordinarily we read files sequentiallyScanner scan ;// Instantiate a Scanner scan for the “file” belowchar c = ‘’;while (c != ‘s’) {
c = scan.nextChar(); }
scan
Are 10^15 Files Just a Peta-File?
![Page 17: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/17.jpg)
Typical File I/O
Ordinarily we read files sequentiallyScanner scan ;// Instantiate a Scanner scan for the “file” belowchar c = ‘’;while (c != ‘s’) {
c = scan.nextChar(); }
scan
Are 10^15 Files Just a Peta-File?
![Page 18: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/18.jpg)
Typical File I/O
Ordinarily we read files sequentiallyScanner scan ;// Instantiate a Scanner scan for the “file” belowchar c = ‘’;while (c != ‘s’) {
c = scan.nextChar(); }
scan
Are 10^15 Files Just a Peta-File?
![Page 19: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/19.jpg)
Writing a Text File
Writing a text file only slightly more complicated Console is file in Unix, so can guess where
this goes Need to first decide what should
happen to file Easy if file does not exist create file &
write to it Else what should happen to file's current
contents? Mode used at opening determines file's
contents If opening file in write mode, erases file at
the start Starts at end of file in append mode, saving
the data
![Page 20: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/20.jpg)
Opening File For Writing
Create instance of java.io.FileWriter Must specify mode to open file at this time Be very careful with this – there is no undo
here! If file is impossible and so cannot be
written to Cannot be done, so system throws
IOException Not told if file existed before this command
FileWriter nuked=new FileWriter("boom.t", false);FileWriter saved =
new FileWriter("ScoreOnRebound",true);
![Page 21: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/21.jpg)
Second Step To Writing Files FileWriter helps, but slow and hard to
use Faster, simpler approach would be much
nicer Using FileWriter create BufferedWriter Cannot change mode; must take care
initially Two methods used to write out data to
file Both methods will expand file & advance
pointer Start writing new line – newLine() write(String s) – writes s to file
End writing & save results with close()
![Page 22: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/22.jpg)
Writing a Text File
try {FileWriter fw = new FileWriter(“b.t”, true);BufferedWriter bw = new BufferedWriter(fw);for (int i = 10; i > 0; i--) { bw.write(“T minus ”); bw.write(i + “”); bw.newLine();}bw.write(“Blast off!”); bw.close();
} catch (IOException ioe) {ioe.printStackTrace();
}
![Page 23: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/23.jpg)
Its Not All Text
We often want to store more than just text Translate numbers into binary to be used in
program Storing as text wastes time converting back
& forth (Often) Space also wasted for larger numbers
Could instead store numbers in binary format Optimized for machine, as not easily human-
readable But how often do we look at numbers in
image file? Easy to determine sizes; each type has
specific length To enable binary formats, use different File class
![Page 24: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/24.jpg)
RandomAccessFile
Built into Java's standard set of classes Found in the java.io package
New or existing files can be accessed with itRandomAccessFile raf = new RandomAccessFile("f.txt","rw");
First argument ("f.txt") is name of file used Access to file specified ("rw") in second
parameter Using write access ("w") erases any data in
the file Read & write anywhere in file using
instance
![Page 25: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/25.jpg)
Reading RandomAccessFile
Defines methods to read most primitive types:
boolean readBoolean()int readInt()double readDouble()
Reads & returns value read from file
Binary encoding used automatically File will store 32-bit int, not "125" Not human readable, but not really needed Can shrink files; always makes sizes
predictable
![Page 26: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/26.jpg)
Reading RandomAccessFile
Reading Strings takes a little extra work
String readUTF() Requires that String was recorded in UTF
format Not totally readable, but makes sense to
machines Or use readChar() to read in String… …but need null character ('\0') at end
End of String not easy to find without some hint
Also remember that Java’s char not always readable
readByte() is readable, but needs typecast
![Page 27: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/27.jpg)
Writing RandomAccessFile
Also defines methods to write to a file:void writeInt(int i)
void writeDouble(double d)void writeUTF(String s)
Writes value at location in the file we are currently at
As it is needed, methods extend file also When writing data, erases anything there
previously
![Page 28: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/28.jpg)
RandomAccessFile I/O
Unless specified still read &write sequentiallyRandomAccessFile raf = new …;char c = ‘’;while (c != ‘s’) {
c = (char)raf.readByte();raf.writeByte((byte)c);
}Could I rename machine "PetaHertz"
![Page 29: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/29.jpg)
Skipping Around The File
RandomAccessFile allows moving in the file Skip past sections using int skipBytes(int n)
void seek(long pos) moves to position in file
Positions specified as bytes from beginning of file
![Page 30: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/30.jpg)
RandomAccessFile I/O
Sequential access is no longer requiredRandomAccessFile raf = new …; char c;raf.skipBytes(raf.length()-1);c = (char)raf.readByte();raf.seek(0);raf.writeByte((byte)c);
Could I rename machine "PetaHertz"
![Page 31: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/31.jpg)
RandomAccessFile I/O
Sequential access is no longer requiredRandomAccessFile raf = new …; char c;raf.skipBytes(raf.length()-1);c = (char)raf.readByte();raf.seek(0);raf.writeByte((byte)c);
Could I rename machine "PetaHertz"
![Page 32: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/32.jpg)
RandomAccessFile I/O
Sequential access is no longer requiredRandomAccessFile raf = new …; char c;raf.skipBytes(raf.length()-1);c = (char)raf.readByte();raf.seek(0);raf.writeByte((byte)c);
Could I rename machine "PetaHertz"
![Page 33: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/33.jpg)
RandomAccessFile I/O
Sequential access is no longer requiredRandomAccessFile raf = new …; char c;raf.skipBytes(raf.length()-1);c = (char)raf.readByte();raf.seek(0);raf.writeByte((byte)c);
Could I rename machine "PetaHertz"
![Page 34: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/34.jpg)
RandomAccessFile I/O
Sequential access is no longer requiredRandomAccessFile raf = new …; char c;raf.skipBytes(raf.length()-1);c = (char)raf.readByte();raf.seek(0);raf.writeByte((byte)c);
"ould I rename machine "PetaHertz"
![Page 35: Lecture 20: Java File I/o](https://reader036.fdocuments.net/reader036/viewer/2022062520/568161a3550346895dd15bc9/html5/thumbnails/35.jpg)
For Next Lecture
Angel still shows no weekly assignment Will be returning this week; no new rules will
apply Will talk about indexed files on
Wednesday Why does Oracle love them & heavily rely on
them? Why does CS department require you to
learn them? Why do you feel like they are key idea for
project #2?