Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.

Similar presentations


Presentation on theme: "1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions."— Presentation transcript:

1 1 Recitation 8

2 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions

3 3 Setup Create a text file called temp.txt Type some lines into it eg. The Quick Brown Fox The Slow Three-toed Sloth The Furious Rabid Chihuahua We’ll write code to load the file

4 4 Setup Create the class MyFileLoader.java Create a main class that will contain a main function (or put a static main function into MyFileLoader.java)

5 5 MyFileLoader.java public class MyFileLoader { public MyFileLoader(String filename) { BufferedReader reader = new BufferedReader(new FileReader(filename)); reader.close(); }... } The FileReader class allows you to read from a file using the read() function The read() functions, however, are a bit awkward to use. They require specifying how many characters you want to read

6 6 MyFileLoader.java public class MyFileLoader { public MyFileLoader(String filename) { BufferedReader reader = new BufferedReader(new FileReader(filename)); reader.close(); }... } Instead of reading characters, we’d like to read entire lines. As a result, we’ll use a BufferedReader, which takes a FileReader as an argument in its constructor BufferedReader opens a file once you call the constructor. If you open a file, remember to close it with the close() function.

7 7 MyFileLoader.java public MyFileLoader(String filename) { BufferedReader reader = new BufferedReader(new FileReader(filename)); String line = reader.readLine(); while( line != null ) { System.out.println(line); line = reader.readLine(); } reader.close(); } To read a line of text from a file, use BufferedReader’s readLine() function It returns the line as a String If you hit the end of the file, readLine() returns null

8 8 MyFileLoader.java public MyFileLoader(String filename) throws FileNotFoundException, IOException { BufferedReader reader = new BufferedReader(new FileReader(filename)); String line = reader.readLine(); while( line != null ) { System.out.println(line); line = reader.readLine(); } reader.close(); } There are unhandled exceptions FileNotFoundException and IOException. We need to add a “throws” clause to the MyFileLoader() declaration to tell functions that use this constructor that this constructor throws these exceptions.

9 9 MyFileLoader.java public MyFileLoader(String filename) throws FileNotFoundException, IOException { BufferedReader reader = new BufferedReader(new FileReader(filename)); String line = reader.readLine(); while( line != null ) { String[] tokens = line.split(" "); System.out.println(line); line = reader.readLine(); } reader.close(); } To split each line into words, use the String class split() function. The split function takes an argument which describes what to split on (in reality, it is a regular expression but you haven’t learned about them yet)

10 10 MyFileLoader.java public MyFileLoader(String filename) throws FileNotFoundException, IOException { BufferedReader reader = new BufferedReader(new FileReader(filename)); String line = reader.readLine(); while( line != null ) { String[] tokens = line.split(" "); for( int i = 0; i < tokens.length; i++ ) { System.out.println(tokens[i]); } line = reader.readLine(); } reader.close(); } Now you can print out each word in a line!

11 MyFileLoader.java public static void main(String[] args) { try { MyFileLoader loader = new MyFileLoader(args[0]); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.out.println(e.getMessage()); e.printStackTrace(); } You need to change main to “catch” these “thrown” exceptions

12 12 MyFileLoader.java public static void main(String[] args) { try { MyFileLoader loader = new MyFileLoader(args[0]); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } Or if you don’t care about what type of exceptions they are, you can just handle the generic Exception class

13 Reading input from the Console For Assn #5, you will need to read input from the console To do this, you will need a BufferedReader object as follows: 13 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String text = in.readLine(); This will prompt you to type something in the console

14 14 Command Line Arguments public static void main(String[] args) { try { MyFileLoader loader = new MyFileLoader(args[0]); }... } This is called a command line parameter because you supply it on the command line (which means it’s from outside the program) If you run java from a command prompt, you could pass this parameter when you type in: java MyFileLoader temp.txt

15 15 Command Line Arguments When running your code from Eclipse, you do it a little differently Go to Run->Run … Select the java file with the main function in the “configurations” Click on the “Arguments” tab Enter the command line argument (in this case, it will be the temp.txt file)

16 16 Command Line Arguments Enter it here

17 17 Command Line Arguments Eclipse assumes the default directory for the files is the project directory You can also specify the full path name eg. “C:\courses\cs162\recitation8\” Note that you can put quotes in there just in case Eclipse doesn’t like path names with spaces eg. My Documents

18 18 Run your program! You should see the contents of temp.txt being printed out on the console

19 19 Things you can do in the remainder of recitation: 1.Try designing your classes for Assignment #5 2.Try loading the questions file


Download ppt "1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions."

Similar presentations


Ads by Google