Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10 Exceptions and File I/O. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Exceptions Exception handling is an important aspect of object-oriented.

Similar presentations


Presentation on theme: "Chapter 10 Exceptions and File I/O. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Exceptions Exception handling is an important aspect of object-oriented."— Presentation transcript:

1 Chapter 10 Exceptions and File I/O

2 © 2004 Pearson Addison-Wesley. All rights reserved10-2 Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on:  the purpose of exceptions  exception messages  the try-catch statement  propagating exceptions  the exception class hierarchy  GUI mnemonics and tool tips  more GUI components and containers

3 © 2004 Pearson Addison-Wesley. All rights reserved10-3 Outline Exception Handling The try-catch Statement Exception Propagation Exception Classes I/O Exceptions Tool Tips and Mnemonics Combo Boxes Scroll Panes and Split Panes

4 © 2004 Pearson Addison-Wesley. All rights reserved10-4 File I/O Reading in from a file Java has several classes that handle input from a file. One of these is FileReader. Declaring an object of type FileReader will allow a program to read in one character at a time with the read method But generally, one wants to read in more than one character at a time, say a line at a time. For this we use the BufferedReader class together with the FileReader class. It has the method readLine(), which will read in a line of a file.

5 © 2004 Pearson Addison-Wesley. All rights reserved10-5 File I/O -Reading in from a file Declaring an object of type BufferedReader:  BufferedReader filein=new BufferedReader(new FileReader( “data.dat”));  You can now use the filein object to readin a line of a file with a statement String input = filein.readLine(); Read in an entire file as follows:  BufferedReader filein = new BufferedReader(new FileReader(“data.dat”)); //declaring object to read in  while((str =filein.readLine()) != null) //reading in a line and //checking whether at the end of file  System.out.println(str); //printing out the line Remember the method with these statements will need a throws IOException in the header or you will have to have a try…catch block around them

6 © 2004 Pearson Addison-Wesley. All rights reserved10-6 File I/O Writing out to a file Java has a number of classes that can be used to write data out to a file. We will use two of them together, PrintWriter and FileWriter FileWriter allows writing to a file one character at a time and PrintWriter allows writing a line at a time, if used with FileWriter. The methods that PrintWriter has to write with are print(), and println() Because print() and println(), as we have seen already are overloaded, you can write out ints, doubles, Strings or concatenated combinations.

7 © 2004 Pearson Addison-Wesley. All rights reserved10-7 File I/O Writing out to a file To declare an object of PrintWriter Class : PrintWriter fileout = new PrintWriter(new FileWriter(“dataout.dat”)); To to write a line to your file with this object:  fileout.println(“My name is “+name); To read write out an array of ints, one number per line, after declaring fileout as above:  int arr [] = { 1,2,3,4,5,6,7,8,9,10,11,13,15,17,20,30,23};  for(int index=0;index<arr.length;index++) fileout.println( “arr[index] + “ is on the “ +index + “ line”); Remember, again you would need a throws IOException in the header of the method with these statements or have to surround the statements with a try..catch block.

8 © 2004 Pearson Addison-Wesley. All rights reserved10-8 File I\O -Reading in and Writing out To read in from a file data.dat and write out what you have read in to a file dataout.dat:  BufferedReader input = new BufferedReader(new FileReader(“data.dat”));  PrintWriter output = new PrintWriter(new FileWriter(“dataout.dat”));  String str;  try {  while((str = input.readLine()) != null) output.println(str);  }catch(FileNotFoundException fnfe) { System.out.println(fnfe);  }catch(IOException oe) { System.out.println(oe); }}

9 © 2004 Pearson Addison-Wesley. All rights reserved10-9 File I/O - parsing the line of input Once you have read in a line from a data file, say with the statement  BufferedReader input = new BufferedReader(new FileReader(“data.dat”));  String str = input.readLine(); You have a String variable, str, with a line from data.dat. If the line just contains a statement that you want to print out you can just say System.out.println(str); and be finished But suppose instead the line contains a set of numbers That you want to add up and take an average of. In this case you need some way of separating the numbers. In Java, there is a class that allows you to do that, the String Tokenizer class.

10 © 2004 Pearson Addison-Wesley. All rights reserved10-10 Separating the data in a String The String Tokenizer class allows you to parse a String (line) of data and extract each data item in the String. To declare a String Tokenizer object:  StringTokenizer tokenizer = new StringTokenizer(str);  //where str is a line of data read in. So you declare and instantiate a new tokenizer object for each line (String) of data read in from a file. Then you use a loop to search the String str to extract each data item.  while(tokenizer.hasMoreTokens()) { //checking if at end String word = tokenizer.nextToken(); //getting next word numwords++; //counting the words }

11 © 2004 Pearson Addison-Wesley. All rights reserved10-11 Separating the data in a String When you declare a String Tokenizer object like this:  StringTokenizer tokenizer = new StringTokenizer(str); The separator for the data will automatically be a blank, so words or numbers separated by blanks can be extracted You can specify what the separator will be:  StringTokenizer=new StringTokenizer(str, “,”); Now the separator will be a comma.

12 © 2004 Pearson Addison-Wesley. All rights reserved10-12 Reading in an array of data in a file with 10 numbers per line Public static void main(String args[]) throws IOException { BufferedReader input = new BufferedReader(new FileReader(“data.dat”)); //declaring input object int arr [] = new int[10][30]; //declaring array to hold input int I = 0; String str; while((str =input.readLine()) != null) //reading until eof { StringTokenizer tok=new StringTokenizer(str); //data //separated by blanks for(int j = 0; j<arr[I]length;j++) while(tok.hasMoreTokens()) //at end of line arr[ I][j]=Integer.parseInt(tok.nextToken()); I++; } }

13 © 2004 Pearson Addison-Wesley. All rights reserved10-13 File I/O- Reading in text file and counting the words Suppose we have a file with the following words in it: “Out, out, brief candle, life’s but a walking shadow, a poor player who struts and frets his hour upon the stage and then is heard no more; it is a tale told by an idiot, full of sound and fury signifying nothing”

14 © 2004 Pearson Addison-Wesley. All rights reserved10-14 File I/O -reading in text file and counting words cont. Then to read in that file and count the number of words in it: BufferedReader input=new BufferedReader(new FileReader(“data.dat”)); //get an object to use for reading String str; //String variable to hold input int numwords=0; //int to hold word count while((str=input.readLine()) != null) //loop reading in lines { StringTokenizer tok = new StringTokenizer(str); //Tokenizer for //each line while(tok.hasMoreTokens()) //loop for getting each word on //line { tok.nextToken(); //getting next word numwords++; //adding another word to count } }

15 © 2004 Pearson Addison-Wesley. All rights reserved10-15 Summary Chapter 10 has focused on: the purpose of exceptions exception messages the try-catch statement propagating exceptions the exception class hierarchy GUI mnemonics and tool tips more GUI components and containers


Download ppt "Chapter 10 Exceptions and File I/O. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Exceptions Exception handling is an important aspect of object-oriented."

Similar presentations


Ads by Google