Recitation 8 1

advertisement
Recitation 8
1
Outline
Goals of this recitation:
1. Learn about loading files
2. Learn about command line arguments
3. Review of Exceptions
2
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
3
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)
4
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
5
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.
6
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
7
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
8
constructor throws these exceptions.
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)
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(" ");
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!
10
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
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
12
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:
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
String text = in.readLine();
This will prompt you to type something in the console
13
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
14
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)
15
Command Line Arguments
Enter it here
16
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
17
Run your program!
• You should see the contents of temp.txt
being printed out on the console
18
Things you can do in the
remainder of recitation:
1. Try designing your classes for
Assignment #5
2. Try loading the questions file
19
Download