Java_IO_files_13
Java_IO_files_13
InputStream
byte
Class
Host Machine These classes are Java Program
ASCII? tailored to a specific Using Bytes
EBCDIC? host machine.
byte
OutputStream
Class
Readers and Writers
Reader Class
InputStream
Class char
Host Machine
Java Program
ASCII?
Using Unicode
EBCDIC?
Writer Class
char
OutputStream
Class
Streams
BufferedReader
adds readLine()
and buffering
“looks like” a Stream InputStreamReader
“looks like” a Reader
System.in
(InputStream)
adds read()
“looks like” a Stream
“looks like” a Reader an abstract class
java.io
• “Throws” checked exceptions try-catch
statement should be used to handle code
that throws checked exceptions.
Byte streams
• Two parent abstract classes: InputStream and
OutputStream
• Reading bytes:
– InputStream class defines an abstract method
public abstract int read() throws IOException
• Designer of a concrete input stream class overrides
this method to provide useful functionality.
• E.g. in the FileInputStream class, the method reads
one byte from a file
– OutputStream class defines an abstract method
public abstract void write(int b) throws IOException
Example code1:
import java.io.*;
class CountBytes {
public static void main(String[] args)
throws IOException {
FileInputStream in = new
FileInputStream(args[0]);
int total = 0;
while (in.read() != -1)
total++;
in.close();//Always close streams
System.out.println(total + ”bytes”);
}
}
Example code2:
import java.io.*;
class TranslateByte {
public static void main(String[] args)
throws IOException {
byte from = (byte)args[0].charAt(0);
byte to = (byte)args[1].charAt(0);
byte x;
while((x = System.in.read()) != -1)
System.out.write(x == from ? to :
x);
}
}
If you run “java TranslateByte b B” and enter text
bigboy via the keyboard the output will be: BigBoy
Character streams
• Two parent abstract classes for characters:
Reader and Writer.
• The standard streams—System.in, System.out
and System.err—existed before the invention of
character streams. So they are byte streams though
logically they should be character streams.
Stream Objects
All Java programs make use of standard stream
objects
• System.in
– To input bytes from keyboard
• System.out
– To allow output to the screen
• System.err
– To allow error messages to be sent to screen
Conversion between byte and character streams
Writing
output = new ObjectOutputStream
( new FileOutputStream(filename ));
output.writeObject( objectname );
output.close( );
Reading
input = new ObjectInputStream
new FileInputStream( filename ) );
record = ( ObjectType ) input.readObject( );
input.close( );
The File class
• The File class is particularly useful for retrieving
information about a file or a directory from a disk.
– A File object actually represents a path, not
necessarily an underlying file
– A File object doesn’t open files or provide any file-
processing capabilities
• Three constructors
– public File( String name)
– public File( String pathToName,
String name)
– public File( File directory,
String name)
Methods in the File class
– boolean canRead() / boolean
canWrite()
– boolean exists()
– boolean isFile() / boolean
isDirectory() / boolean isAbsolute()
– String getAbsolutePath() / String
getPath()
– String getParent()
– String getName()
– long length()
– long lastModified()