Title: File Input and Output
1File Input and Output
- A program that outputs data to a disk file, exits
and then re-starts, can access the data again, by
inputting it from file. - File input
- read data from files
- File output
- write data back to files
2User - input/output
- A program can input data from the user, process
the data and output some results to the screen. - However, if you run the program once, obtain the
data, exit the program and then start the program
again, all of the data is lost.
3- File
- Instances of this class represent the name of a
file or directory on the host file system - FileInputSteam/FileOutputStream
- inputStream/outputSteam
- can be used to read/write bytes into a file
- Higher levels of input/output classes
- BufferedReader
- PrintStream
4The File class
- Java has a class called File whose instances are
used to represent disk files. - You can create a File object with a given file
name - File phoneFile new File(phone.dat)
- Messages exist(), mkdir(), renameTo(F)
- However, you cannot create a disk file using a
File object, or read or write data to a disk file
directly!
5FileOutputStream Class
- To create a disk file and to write to it, you
need to create an instance of FileOutputStream
on the File object - FileOutputStream output
- output new FileOutputStream(phone.dat )
- If the file phone.dat did not exist then it will
be created - If the file alreay existed, it will be emptied.
6import java.io. / This program print out the
given file on the screen. Uses java List
fileName / public class List public static
void main(String args) throws IOException
int aInt FileInputStream inputfile new
FileInputStream (args0) aInt
inputfile.read() while (aInt gt 0)
System.out.print((char)aInt) aInt
inputfile.read()
7Import java.io. public class lower public
static void main(string args) throws
ioexception int aint fileinputstream
inputfile new fileinputstream (args0)
fileoutputstream outputfile new
fileoutputstream (args1) aint
inputfile.read() while (aint gt 0)
if (aint gt64 aint lt 90)
outputfile.write( (char) aint32) else
outputfile.write((char)aint) aint
inputfile.read()
8Higher Level of input/output streams
- Unfortunately, the only thing you can do with a
FileOutput(Input)Stream is to output( input )
bytes. - We usually want to output Strings or ints or
other more interesting objects and values. - To do this we create a higher level of
input/output steam object on the
FileOutputStream ( FileInputStream )
9import java.io. / just to demo how to use
PrintWriter / public class WriterDemo public
static void main(String args) throws
IOException PrintWriter out new
PrintWriter( new FileOutputStream
("writer.dat" ) ) out.println("this is just
a test") out.println("one more test
string") out.flush()
10import java.io. / just to demo how to use
BufferReader Uses java List fileName
/ public class List2 public static void
main(String args) throws IOException
String aString FileInputStream inputFile
new FileInputStream (args0)
InputStreamReader inputReader new
InputStreamReader(inputFile) BufferedReader
input new BufferedReader( inputReader )
aString input.readLine() while ( aString
! null ) System.out.println(aString)
aString input.readLine()
11Questions
- How to store a collection of objects in a file?
- How to update a file?
- How to sequentially access a list of objects in a
file - How to randomly access an object in a file?