25
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

java.io supports console and file I/O

  • Upload
    tex

  • View
    63

  • Download
    0

Embed Size (px)

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

Page 1: java.io supports console and file I/O

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

Page 2: java.io supports console and file I/O

Stream I/O

• byte stream classes - raw data

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

• Object stream classes

Page 3: java.io supports console and file I/O

Byte Streams

ByteArrayOutputStream

OutputStream FileOutputStream

FilterOutputStream

ByteArrayInputStream

FileInputStream

InputStream FilterInputStream

Abstractsuperclasses

* These subclasses may be superclasses themselves

Page 4: java.io supports console and file I/O

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

Page 5: java.io supports console and file I/O

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

Page 6: java.io supports console and file I/O

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

Page 7: java.io supports console and file I/O

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

Page 8: java.io supports console and file I/O

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();

Page 9: java.io supports console and file I/O

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!!

Page 10: java.io supports console and file I/O

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. ………

Page 11: java.io supports console and file I/O

• Some more File methods » delete » renameTo

Page 12: java.io supports console and file I/O

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

Page 13: java.io supports console and file I/O

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.

Page 14: java.io supports console and file 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)

Page 15: java.io supports console and file I/O

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);

Page 16: java.io supports console and file I/O

Character Stream Input

The FileReader class extends InputStreamReader..

and so it IS an InputStreamReader

Reader InputStreamReader

FileReader

Page 17: java.io supports console and file I/O

Same concepts apply to file

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

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

Page 18: java.io supports console and file I/O

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);

Page 19: java.io supports console and file I/O

Character Stream Input

The FileReader class extends InputStreamReader..

and so it IS an InputStreamReader

Writer InputStreamWriter

FileWriter

Page 20: java.io supports console and file I/O

Code to Write a Character to Disk

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

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

Page 21: java.io supports console and file I/O

PrintWriter class has println•

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

out.println(“goodbye”);

Page 22: java.io supports console and file I/O

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 )

Page 23: java.io supports console and file I/O

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

Page 24: java.io supports console and file I/O

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;

Page 25: java.io supports console and file I/O

A JFileChooser Dialog