Reading From a File

To implement a file reader starting from scratch, first of all, rename the class.

Reading From a File

To implement a file reader starting from scratch, first of all, rename the class.

Reading From a File

To implement a file reader starting from scratch, first of all, rename the class.

Reading From a File

Any program which does I/O needs to use the facilities of the I/O package

Reading From a File

Any program which does I/O needs to use the facilities of the I/O package

Reading From a File

Any program which does I/O needs to use the facilities of the I/O package

Reading From a File

To 'cat' all the files, we'll have to loop around all the arguments. Note that for the loop bound we use the array's explicit length which is stored as part of every array or string.

Reading From a File

To 'cat' all the files, we'll have to loop around all the arguments. Note that for the loop bound we use the array's explicit length which is stored as part of every array or string.

Reading From a File

To 'cat' all the files, we'll have to loop around all the arguments. Note that for the loop bound we use the array's explicit length which is stored as part of every array or string.

Reading From a File

The way to handle files in Java is to set up a File object which "wraps" each file name. A File has methods for checking its existence and the settings of its attributes.

Reading From a File

The way to handle files in Java is to set up a File object which "wraps" each file name. A File has methods for checking its existence and the settings of its attributes.

Reading From a File

The way to handle files in Java is to set up a File object which "wraps" each file name. A File has methods for checking its existence and the settings of its attributes.

Reading From a File

A File object can be used as the 'seed' for a new Reader object. In general there are many sorts of Readers which are all used for reading the contents of their particular objects.

Reading From a File

A File object can be used as the 'seed' for a new Reader object. In general there are many sorts of Readers which are all used for reading the contents of their particular objects.

Reading From a File

A File object can be used as the 'seed' for a new Reader object. In general there are many sorts of Readers which are all used for reading the contents of their particular objects.

Reading From a File

What we specifically need here is something that reads files. That something is a FileReader...

Reading From a File

What we specifically need here is something that reads files. That something is a FileReader...

Reading From a File

What we specifically need here is something that reads files. That something is a FileReader...

Reading From a File

...and since all Readers have character-reading capabilities we can take advantage of it a very familiar way.
NB Readers treat their contents as Unicode character streams whereas the old-fashioned InputStreams from Java 1.0 treat their contents as byte streams (possibly with an ASCII interpretation).

Reading From a File

...and since all Readers have character-reading capabilities we can take advantage of it a very familiar way.
NB Readers treat their contents as Unicode character streams whereas the old-fashioned InputStreams from Java 1.0 treat their contents as byte streams (possibly with an ASCII interpretation).

Reading From a File

...and since all Readers have character-reading capabilities we can take advantage of it a very familiar way.
NB Readers treat their contents as Unicode character streams whereas the old-fashioned InputStreams from Java 1.0 treat their contents as byte streams (possibly with an ASCII interpretation).

Reading From a File

Unfortunately, there are many tings that can go wrong. The simplest way to cover all eventualities is to declare that the program throws miscellaneous I/O exceptions. That covers a multitude of sins like non-existing files, problems with permissions and disk crashes.

Reading From a File

Unfortunately, there are many tings that can go wrong. The simplest way to cover all eventualities is to declare that the program throws miscellaneous I/O exceptions. That covers a multitude of sins like non-existing files, problems with permissions and disk crashes.

Reading From a File

Unfortunately, there are many tings that can go wrong. The simplest way to cover all eventualities is to declare that the program throws miscellaneous I/O exceptions. That covers a multitude of sins like non-existing files, problems with permissions and disk crashes.

Reading From a File

Strictly speaking, that's fine and the code is fully functional. If there is an 'exception situation', the Java Machine will terminate the program and print an error trace on the terminal. This is not very user-friendly: on second thoughts we'd better handle the errors.

Reading From a File

Strictly speaking, that's fine and the code is fully functional. If there is an 'exception situation', the Java Machine will terminate the program and print an error trace on the terminal. This is not very user-friendly: on second thoughts we'd better handle the errors.

Reading From a File

Strictly speaking, that's fine and the code is fully functional. If there is an 'exception situation', the Java Machine will terminate the program and print an error trace on the terminal. This is not very user-friendly: on second thoughts we'd better handle the errors.

Reading From a File

We do this by enclosing the statements which may generate an error (which is most of them!) in a try/catch combo. In effect we are going to try to execute those statements, but if some error condition arises, we will catch it and do something about it.

Reading From a File

We do this by enclosing the statements which may generate an error (which is most of them!) in a try/catch combo. In effect we are going to try to execute those statements, but if some error condition arises, we will catch it and do something about it.

Reading From a File

We do this by enclosing the statements which may generate an error (which is most of them!) in a try/catch combo. In effect we are going to try to execute those statements, but if some error condition arises, we will catch it and do something about it.

Reading From a File

We do this by enclosing the statements which may generate an error (which is most of them!) in a try/catch combo. In effect we are going to try to execute those statements, but if some error condition arises, we will catch it and do something about it.

Reading From a File

We do this by enclosing the statements which may generate an error (which is most of them!) in a try/catch combo. In effect we are going to try to execute those statements, but if some error condition arises, we will catch it and do something about it.

Reading From a File

The it that we catch is actually an IOException...

Reading From a File

The it that we catch is actually an IOException...

Reading From a File

The it that we catch is actually an IOException...

Reading From a File

...and we will just print out an error message and carry on

Reading From a File

...and we will just print out an error message and carry on

Reading From a File

...and we will just print out an error message and carry on

Reading From a File

The finished code!

public class HelloWorld{

public static void main(String[] arg){
	System.out.println("Hello World");
	}

}
public class HelloWorld{

public static void main(String[] arg){
	System.out.println("Hello World");
	}

}
public class Cat{

public static void main(String[] arg){
	System.out.println("Hello World");
	}

}
public class Cat{

public static void main(String[] arg){
	System.out.println("Hello World");
	}

}
*public class Cat{

public static void main(String[] arg){
	System.out.println("Hello World");
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	System.out.println("Hello World");
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	System.out.println("Hello World");
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	System.out.println("Hello World");
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new ?(f);
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new ?(f);
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new ?(f);
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		...
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) throws IOException {
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) throws IOException {
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) throws IOException {
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
	*	File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
	    try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
	    try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	*}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
	    try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(?){
		...
		}
	    }}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
	    try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(?){
		...
		}
	    }}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
	    try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(?){
		...
		}
	    }}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
	    try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){
		...
		}
	    }}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
	    try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){
		...
		}
	    }}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
	    try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){
		...
		}
	    }}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
	    try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){
		System.err.println(e);
		}
	    }}

}
import java.io.*;

public class Cat{

public static void main(String[] arg) {
	for(int i=0; i<arg.length; i++){
	    try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){
		System.err.println(e);
		}
	    }}

}