File I/O in Java
File I/O in Java
File Basics
Recall that a file is block structured. What
does this mean?
What happens when an application opens or
closes a file?
Every OS has its own EOF character and,
for text files, its own EOL character(s).
Streams
Java file I/O involves streams. You write
and read data to streams.
The purpose of the stream abstraction is to
keep program code independent from
physical devices.
Three stream objects are automatically
created for every application: System.in,
System.out, and System.err.
Types of Streams
There are 2 kinds of streams
byte streams
character streams
Character Streams
Character streams create text files.
These are files designed to be read with a
text editor.
Java automatically converts its internal
unicode characters to the local machine
representation (ASCII in our case).
Byte Streams
Byte streams create binary files.
A binary file essentially contains the
memory image of the data. That is, it stores
bits as they are in memory.
Binary files are faster to read and write
because no translation need take place.
Binary files, however, cannot be read with a
text editor.
Classes
Java has 6 classes to support stream I/O
File: An object of this class is either a file
or a directory.
OutputStream: base class for byte output
streams
InputStream: base class for byte input
streams
Writer: base class for character output
streams.
Reader: base class for character input
streams.
RandomAccessFile: provides support for
random access to a file.
Note that the classes InputStream,
OutputStream, Reader, and Writer are
abstract classes.
File class
File myDir = new File(C:\\CS311);
See FileWrite.java
Reading One Char at a Time
See StreamReader.java
The read() method returns an integer
This integer should be cast to a char
A value of -1 indicates the end of the stream
has been reached.
Reading One Line at a Time
See LineReader.java
Use a BufferedReader
The readLine() method returns a String.
If the String is null, then the end of the
stream has been reached.
Reading One Word at a Time
See WordReader.java
Use a StreamTokenizer
ttype: an int that contains the type of the
current token. Values are TT_EOF, TT_EOL,
TT_WORD, TT_NUMBER, or a character.
sval: String containing the current token if it is
a word
nval: double containing the current token if it is
a number
Reading and Writing Binary Files
Reading and Writing Binary Files
To read and write binary files, use
DataInputStream and DataOutputStream
DataOutputStream Methods
writeByte( int value )
writeBoolean( boolean value )
writeChar( int value )
writeShort( int value )
writeInt( int value )
writeLong( long value )
writeFloat( float value )
writeDouble( double value )
String Output
Writing Strings
writeBytes( String s ) //for Strings
write( byte[] b, int offset,
int length ) //partial strings
A String may be converted to a byte
array by using the String method
getBytes()
If you use writeChars( String s ) you
will get Unicode characters (2 bytes).
Other Methods
flush()
size() //number of bytes written
close()