2
2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors. The basic I/O related exception classes are given below: EOFException – signals that end of the file is reached unexpectedly during input. FileNotFoundException – file could not be opened InterruptedIOException – I/O operations have been interrupted IOException – signals that I/O exception of some sort has occurred – very general I/O exception.

6
6 Reading and Writing Characters As pointed out earlier, subclasses of Reader and Writer implement streams that can handle characters. The two subclasses used for handling characters in file are: FileReader FileWriter While opening a file, we can pass either file name or File object during the creation of objects of the above classes.

8
8 Reader - operations public int read()Reads a character and returns as a integer 0-255 public int read(char[] buf, int offset, int count) Reads and stores the characters in buf starting at offset. count is the maximum read. public int read(char[] buf)Same as previous offset=0 and length=buf.length() public long skip(long count)Skips count characters. public boolean()Returns true if the stream is ready to be read. public void close()Closes stream

12
12 Copying Characters from Files Write a Program that copies contents of a source file to a destination file. The names of source and destination files is passed as command line arguments. Make sure that sufficient number of arguments are passed. Print appropriate error messages.

15
15 Buffered Streams Java supports creation of buffers to store temporarily data that read from or written to a stream. This process is known as buffered I/O operation. Buffered stream classes – BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter buffer data to avoid every read or write going to the stream. These are used in file operations since accessing the disk for every character read is not efficient.

16
16 Buffered Streams Buffered character streams understand lines of text. BufferedWriter has a newLine method which writes a new line character to the stream. BufferedReader has a readLine method to read a line of text as a String. For complete listing of methods, please see the Java manual/documentation.

17
17 BufferedReader - example Use a BufferedReader to read a file one line at a time and print the lines to standard output import java.io.*; class ReadTextFile { public static void main(String[] args) throws FileNotFoundException, IOException { BufferedReader in; in = new BufferedReader( new FileReader(Command.txt)); String line; while (( line = in.readLine()) != null ) { System.out.println(line); }

18
18 Reading/Writing Bytes The FileReader and FileWriter classes are used to read and write 16-bit characters. As most file systems use only 8-bit bytes, Java supports number of classes that can handle bytes. The two most commonly used classes for handling bytes are: FileInputStream (discussed earlier) FileOutputStream