Presentation is loading. Please wait.

Presentation is loading. Please wait.

java.io supports console and file I/O

Similar presentations


Presentation on theme: "java.io supports console and file I/O"— Presentation transcript:

1 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

2 Stream I/O byte stream classes - raw data character stream classes - do not assume that a character is one byte Object stream classes

3 Byte Streams ByteArrayOutputStream OutputStream FileOutputStream
Abstract superclasses ByteArrayOutputStream OutputStream FileOutputStream FilterOutputStream ByteArrayInputStream FileInputStream InputStream FilterInputStream * These subclasses may be superclasses themselves

4 Input Byte Streams InputStream FilterInputStream BufferedInputStream
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.

5 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

6 Output Byte Streams OutputStream FilterOutputStream PrintStream
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.

7 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

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

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

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

11 Some more File methods delete renameTo

12 character stream classes - do not assume that a character is one byte
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

13 Character Streams BufferedWriter Writer OutputStreamWriter PrintWriter
Abstract superclasses BufferedWriter Writer OutputStreamWriter PrintWriter BufferedReader Reader InputStreamReader Obviously, we don’t always want to use byte I/O. Java classes InputStreamReader and OutputStreamReader provide the ‘character’ I/O.

14 InputStreamReader reader = new InputStreamReader(System.in);
//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)

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

16 Character Stream Input
Reader InputStreamReader FileReader The FileReader class extends InputStreamReader.. and so it IS an InputStreamReader

17 Same concepts apply to file
FileReader reader = new FileReader("input.txt"); int next = reader.read() ; if (next != -1) char c = (char)next(); ... reader.close()

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

19 Character Stream Input
Writer InputStreamWriter FileWriter The FileReader class extends InputStreamReader.. and so it IS an InputStreamReader

20 Code to Write a Character to Disk
FileWriter writer = new FileWriter("output.txt"); char c=''; writer.write(c); write.close();

21 PrintWriter class has println
FileWriter writer = new FileWriter(“output.txt”) PrintWriter out = new PrintWriter(writer); out.println(“goodbye”);

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

23 If the user chooses a file: JFileChooser.APPROVE_OPTION is returned
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

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

25 A JFileChooser Dialog


Download ppt "java.io supports console and file I/O"

Similar presentations


Ads by Google