Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10: I/O Streams Input Streams Output Streams

Similar presentations


Presentation on theme: "Chapter 10: I/O Streams Input Streams Output Streams"โ€” Presentation transcript:

1 Chapter 10: I/O Streams Input Streams Output Streams
File streams (File input and File) Filters (Data input and output) File class FileDialog class

2 Input/Output Cannot expect user to enter all information when starting program (airline program -- enter all flight info??) Stream - a sequence of characters (either as input or output that we can perform operations to) write a char read a char read a sequence of chars

3 Streams generally connected to files (using FileInputStream or FileOutputStream)
Can also be connected to other objects (Strings, other streams, etc.) DataInputStream and DataOutputStream are streams that allow more complex manipulations Include java.io.*;

4 InputStream Abstract class Methods:
int available() throws IOException - returns 0, can be overridden, indicates # available chars void close() throws IOException - close stream long skip(long n) throws IOException - skip the next n characters

5 InputStream methods: int read() throws IOException - gets the next available char from the stream, -1 if none int read(byte[] bArray) throws IOException, NullPointerException - trys to fill array, returns the number of characters read int read(byte[] bArray, int offset, int length) throws IOException, NullPointerException, IndexOutOfBoundsException - trys to fill array starting at position offset with up to length chars, returns the number read

6 OutputStream Abstract class Methods:
void close() throws IOException - close stream long flush() throws IOException - flush buffer void write(int c) throws IOException - write char c to stream void write(byte[] bArray) throws IOException, NullPointerException - writes the array of chars void write(byte[] bArray, int offset, int length) throws IOException, NullPointerException, IndexOutOfBoundsException - writes length chars starting at offset to stream

7 FileInputStream InputStream connected to a file constructors: methods:
FileInputStream(String fname) throws FileNotFoundException, SecurityException - opens InputStream connected to fname FileInputStream(File f) throws FileNotFoundException, SecurityException - same as above using File class (more later) methods: implements methods of InputStream: available, close, skip, 3 reads

8 FileOutputStream OutputStream connected to a file constructors:
FileOutputStream(String fname) throws FileNotFoundException, SecurityException - opens OutputStream connected to fname FileOutputStream(File f) throws FileNotFoundException, SecurityException - same as above using File class (more later) methods: implements methods of OutputStream: close, flush, 3 writes

9 void copyFile(String srcF, String dstF) {
try { FileInputStream inf = new FileInputStream(srcF); FileOutputStream outf = newFileOutputStream(dsfF); int ch; while ((ch = inf.read()) != -1) outf.write(ch); inf.close(); outf.close(); } catch (Exception e) { // Announce unable to open files

10 Other InputStream Classes
ByteArrayInputStream - connected to an array of bytes StringBufferInputStream - connected to a string of characters PipedInputStream - connected to a pipe command

11 Other Output Stream Classes
ByteArrayOutputStream - connected to an array of bytes PipedOutputStream - feeding chars to a pipe command

12 Filter Classes Java also provides filter classes
These classes are connected to an input or output stream and allow interpretation of the input/output Most prevalent are DataInputStream and DataOutputStream

13 DataInputStream Connected to an InputStream (often file) Constructor:
DataInputStream(InputStream in) Methods: boolean readBoolean() throws IOException - reads info corresponding to a boolean (1 byte) similar routines for Byte, Char, Double, Float, Int, Long, Short

14 DataInputStream (cont)
More methods: int readFully(byte[] bArray) throws IOException, NullPointerException - read in array of chars stopping when full or at EOF String readLine() throws IOException - read as a string the next sequence of chars until a newline encountered void skipBytes(int n) throws IOException - skip the next n bytes

15 DataOutputStream Connected to an OutputStream (often file)
Constructor: DataOutputStream(OutputStream in) Methods: void writeBoolean(boolean b) throws IOException - writes b to file similar routines for Byte, Char, Double, Float, Int, Long, Short

16 DataOutputStream (cont)
More methods: void writeBytes(byte[] barray) throws IOException - write array of bytes void writeBytes(String s) throws IOException - write string as array of bytes void writeChars(String s) throws IOException - similar to writeBytes for string arg

17 class Employee { private int idnum; private float salary; private String name; void writeToFile(DataOutputStream s) throws IOException { s.writeInt(idnum); s.writeFloat(salary); s.writeString(name + โ€œ\nโ€); } void readFromFile(DataInputStream s) throws IOException idnum = s.readInt(); salary = s.readFloat(); name = s.readLine();

18 void writeEmployeeFile(String fname, Employee[] bees, int numEmployees) {
try { FileOutputStream fos = new FileOutputStream(fname); DataOutputStream fstream = new DataOutputStream(fos); int e; fstream.writeInt(numEmployees); for (e = 0; e < numEmployees; e++) bees[e].writeToFile(fstream); fstream.close(); } catch IOException { // report that something went wrong

19 int readEmployeeFile(String fname, Employee[] bees) {
try { FileInputStream fis = new FileInputStream(fname); DataInputStream fstream = new DataInputStream(fis); int e; int numEmployees; numEmployees = fstream.readInt(); for (e = 0; e < numEmployees; e++) bees[e].readFromFile(fstream); fstream.close(); return numEmployees; } catch IOException { // report that something went wrong

20 Other Useful Filters BufferedInputStream, BufferedOutputStream - reduces I/O with buffering LineNumberInputStream - adds the feature of knowing the line number as lines read PushbackInputStream - implements a one char โ€œpush backโ€ buffer - good for parsing

21 File Class Mechanism for exploring files (often used to avoid having exceptions thrown when files not found) Constructor: File(String name) - produces a File instance even if the name is not a legal file name

22 Methods: boolean exists() - true if file already exists
boolean isDirectory() - true if directory file boolean isFile() - true if data file boolean canRead() - true if file can be read boolean canWrite() - true if file can be written long lastModified() - returns a number indicating last modification time (no given structure)

23 Methods: long length() - number of bytes in file
boolean equals(Object obj) - true if arg is file with same name String[] list() - returns list of files in directory String[] list(FileNameFilter f) - returns list of acceptable files from directory boolean delete() - attempts to delete, true if success

24 FileNameFilter Interface
Restrict files in File, FileDialog classes Implements one method: boolean accept(File directory, String name) - true if this directory, name acceptable Example class JavaSrcFilter implements FileNameFilter { boolean accept(File d, String s) { if (s.endswith(โ€œ.javaโ€)) return true; else return false; }

25 FileDialog Produces a Dialog box that can be used to select files (look depends on system) In java.awt.FileDialog constants: LOAD, SAVE (modes) constructors: FileDialog(Frame Parent, String mssg) - mssg used to prompt, must have parent frame FileDialog(Frame Parent, String mssg, int mode) - mode is one of FileDialog.LOAD or SAVE, indicates expected use of file

26 Methods: String getDirectory() - string corresponding to directory selected File getFile() - File object corresponding to file selected void setFileNameFilter(FileNameFilter f) - set filter used to select files void setDirectory(String s) - set initial directory void setFile(String s) - set initial file void getFileNameFilter() - get current filter (null if none)


Download ppt "Chapter 10: I/O Streams Input Streams Output Streams"

Similar presentations


Ads by Google