Read Files and
Exception and Flow Charts
File Class:
· New type of class found inside java.io so import java.io.File
· Documentation:
o standard doc: http://docs.oracle.com/javase/7/docs/api/?java/io/File.html
o Clearer tutorial: http://tutorials.jenkov.com/java-io/file.html
· Represents filename and the path. Use forward slash in paths - '/'
· Constructor parameter – a filename with a path
o File f = new File ("c:/users/pepper/documents/myfile.txt");
o File f = new File("myfile.txt"); // expects the file in your bluej project folder
o File f = new File ("subfolder/myfile.txt"); // expects the file in the subfolder of your bluej project folder
o bluej project folder is current directory or working directory
· Methods:
o
exists()
Returns a boolean and Tests whether the file
or directory denoted by this abstract pathname exists.
o
canRead()Returns a boolean and Tests whether the application can read the file
denoted by this abstract pathname.
o
getAbsolutePath() Returns a string of
the absolute pathname string of this abstract pathname.
Open a file with Scanner
·
Scanner throws a
checked exception FileNotFoundException if the file
is not found
o Surround with a try phrase
o indicate your method plans to ignore this checked exception
More on Exceptions:
· You already know:
o Create a new Exception of a known type
o Throw an exception from a method : throw new Exception("bad");
o Catch an exception that is thrown
o Look into the exception for information
· New:
o Choose to ignore a certain type of checked exception when using a method
o Syntax: public static void main()
throws FileNotFoundException
{
Read a file with Scanner
· Same methods as with screen input
· Loop through until the file ends: while( input.hasNext()) {
o Error: NoSuchElementException (along with line number from file)
· Read the correct type
o Error: InputMismatchException
· Deal with new line characters
o Scanner's nextInt does not read the \n
o Scanner's nextLine does read the \n
· Forward only
o To read the file again, just create a new Scanner
· Exercise: read a file and print it
· Exercise: sum the hours an employee works – id followed by name followed by hours until end
Parse the input
· hasNextInt, nextInt pairs
· Read lines of text : hasNextLine and nextLine
· Keeps spaces
· Read a string with Scanner
o Construct the Scanner with the string:
o Scanner stringScanner = new Scanner("1 a");
o Use all normal Scanner methods
§ int a = stringScanner.nextInt();
§ String b = stringScanner.next();
· Delimiters
o useDelimiter method of scanner: - change the separator
§ input.useDelimiter(",") : uses comma instead of space to separate
§ Ex input.useDelimiter("[,|^]"); lets | , and ^ all act as delimiters because anything inside the brackets will work as a delimiter. (This is a regular expression.)
· Exercise: sum the hours an employee works – id followed by name followed by hours until end for many workers
Write to a File
· Create a printstream object to replace "System.out" and use the same print and println methods
· System.out is a printstream object
· File f = new File ("output.out");
· PrintStream output = new PrintStream (f);
· Exercises: Copy a file all in uppercase
Prompt for filename:
· Continue asking until canread
Scanner console = new Scanner(System.in);
System.out.println("Enter file name");
File f = new File(console.nextLine());
while (!f.canRead()){
System.out.println("not a good filename. Enter another");
f = new File(console.nextLine());
}
Zip code example:
· prompt user for zip
· prompt for proximity
· find coordinates for target
· find coordinates for matching zips
o Flow chart
o gliffy
· Formula for distance given 2 lat/long
o Earth's radius * arcos(sin(lat1) sin(lat2) + cos(lat1)cos(lat2)cos(long1 – long2)
o must all be in radians not degrees (and file has degrees)
o Earth's radius is 3956.6 miles.