java.io supports console and file I/O

Preview:

DESCRIPTION

java.io supports console and file I/O. most classes work with I/O streams (have beginning, end, and are read sequentially) Random access is supplied by RandomAccessFile class output formatting is provided by java.text classes byte stream classes - raw data - PowerPoint PPT Presentation

Citation preview

java.io supports console and file I/O• most classes work with I/O streams (have beginning,

end, and are read sequentially)

• Random access is supplied by RandomAccessFile class

• output formatting is provided by java.text classes

• byte stream classes - raw data• character stream classes - do not assume that a

character is one byte• Object stream classes

Stream I/O

• byte stream classes - raw data

• character stream classes - do not assume that a character is one byte

• Object stream classes

Byte Streams

ByteArrayOutputStream

OutputStream FileOutputStream

FilterOutputStream

ByteArrayInputStream

FileInputStream

InputStream FilterInputStream

Abstractsuperclasses

* These subclasses may be superclasses themselves

Input Byte Streams

FilterInputStream acts as a ‘wrapper’ for an InputStream object, which is uses as its basic source of data. Methods of FilterInputStream simply pass requests to the InputStream objetct.

A subclass of FilterInputStream, transforms the data along the way, or provides some additional functionality. For example, BufferedInputStream ‘wraps’ a InputStream and provides ‘buffering’ for efficiency, so that file access is not made for byte access. BufferedInputStream overloads ‘read’ so that in addition to one byte at a time, one byte array can be read.

System.in, is an object of type BufferedInputStream, which is provided by the System class in the java.lang input. System.in ‘wraps’ the InputStream object representins the keyboard.

InputStream FilterInputStream

BufferedInputStream

Byte Console Input

int next = System.in.read() ;char c; if (next != -1)

c = (char)next;

//read() reads one byte at a time,// returning it as an int//(overriding InputStream method

Output Byte Streams

FilterOutputStream acts as a ‘wrapper’ for an OutputStream object, which it uses as its basic depository of data. Methods of FilterOutputStream simply pass requests to the OutputStream objetct.

A subclass of FilterOutputStream, transforms the data along the way, or provides some additional functionality. For example, PrintStream ‘wraps’ a OutputStream and provides an accurate display of data types to the output, in addition to buffering. PrintStream also provides the overloaded println,

which accepts String and byte[] parameters, as well as byte.

System.out, is an object of type PrintStream, which is provided by the System class in the java.lang input. System.out ‘wraps’ the OutputStream object representins the console.

OutputStream FilterOutputStream

PrintStream

Byte Console Output

int next = System.in.read() ;System.out.write(next);System.out.flush();System.out.println(“hello world”);

//println is an overloaded method// which handles the display of// it’s parameter data according// to data type

File I/O (Byte)Programming File I/O is much like programming stream

I/O to and from the console --

once a stream is established, its usage is the same regardless of the source/destination!!

However, a file must be associated with a FileInputStream or FileOutputStream object …

FileInputStream in = new FileInputStream (“in.txt”); BufferedInputStream bufIn = new

BufferedInputStream(in); int bb = bufIn.read();

File I/O (Byte)A FileInputStream can be obtained in another manner…

However, a file must be associated with a FileInputStream or FileOutputStream object …

File f = new File(“in.txt”); FileInputStream in = new FileInputStream(f); BufferedInputStream bufIn = new

BufferedInputStream(in); int bb = bufIn.read();

The file object will allow you to check the status of a file before you open it!!

File f = new File(“in.txt”); if ( ! f.exists() ) System.out.println(“file does not exist”); else if ( ! F.canRead() ) System.out.println(“file cannot be read”); else { FileInputStream in = new FileInputStream(f); BufferedInputStream bufIn = new

BufferedInputStream(in); int bb = bufIn.read(); … etc. ………

• Some more File methods » delete » renameTo

Obviously, we don’t always want to use byte I/O.

Java classes InputStreamReader and OutputStreamReader provide the ‘character’ I/O.

InputStreamReader x = new InputStreamReader(

• character stream classes - do not assume that a character is one byte

• Object stream classes

Character Streams

BufferedWriter

Writer OutputStreamWriter

PrintWriter

BufferedReader

Reader InputStreamReader

Abstractsuperclasses

Obviously, we don’t always want to use byte I/O. Java classes InputStreamReader and OutputStreamReader provide the ‘character’ I/O.

//InputStreamReader constructor parameter is InputStream

InputStreamReader reader = new InputStreamReader(System.in); // now have one ‘character’ at a time //where character is defined //by the current InputStreamReader charset char cc = (char) in.read();

But we might not want to read one char at a time…

BufferedReader is a class which also extends Reader…. public BufferedReader(Reader)

InputStreamReader reader = new InputStreamReader(System.in); // now have one ‘character’ at a time //where character is defined //by the current InputStreamReader charset BufferedReader in = BufferedReader(reader); //wrap in another class

//now have buffered input AND a readLine // method String inputLine = in.readLine(); double x = Double.parseDouble(inputLine);

Character Stream Input

The FileReader class extends InputStreamReader..

and so it IS an InputStreamReader

Reader InputStreamReader

FileReader

Same concepts apply to file

FileReader reader = new FileReader("input.txt"); int next = reader.read() ;if (next != -1)

char c = (char)next(); ... reader.close()

A filereader can also be wrapped in a BufferedReader object..

FileReader reader = new FileReader ("input.txt"); BufferedReader in = new BufferedReader(reader); String inputLine = in.readLine(); double x = Double.parseDouble(inputLine);

Character Stream Input

The FileReader class extends InputStreamReader..

and so it IS an InputStreamReader

Writer InputStreamWriter

FileWriter

Code to Write a Character to Disk

FileWriter writer = new FileWriter("output.txt");

... char c='';... writer.write(c); ... write.close();

PrintWriter class has println•

FileWriter writer = new FileWriter(“output.txt”)PrintWriter out = new PrintWriter(writer);

out.println(“goodbye”);

File Dialogs• Use JFileChooser to let a user supply a file name through a file

dialog

• Construct a file chooser object

• Call its showOpenDialog or showSaveDialog method

(Specify null or the user interface component over which to

pop up the dialog )

File Dialogs

• If the user chooses a file:JFileChooser.APPROVE_OPTION is returned

• If the user cancels the selection:JFileChooser.CANCEL_OPTION is returned

• If a file is chosen, use GetSelectedFile method to obtain a File object describing the file

Code to Use a JFileChooser JFileChooser chooser new JFileChooser(); FileReader in; if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); in = new FileReader(selectedFile); } else in = null;

A JFileChooser Dialog

Recommended