Download Reading Files - Everett Public Schools

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
BUILDING JAVA
PROGRAMS
CHAPTER 6
FILE PROCESSING
1
OBJECTIVES!
• Be able to open a file in a directory you specify
• Be able to avoid a compiler error due to a FileNotFound
exception
2
INPUT/OUTPUT (I/O)
File: A collection of information that is stored on a computer
and assigned a particular name.
import java.io.*;
Create a File object to get info about a file on your drive.
(This doesn't actually create a new file on the hard disk.)
File f = new File("example.txt");
3
METHODS OF FILE OBJECTS
4
READING FILES
To read a file, pass a File when constructing a Scanner
Scanner name = new Scanner(new File("file name"));
Example:
File file = new File("mydata.txt");
Scanner input = new Scanner(file);
or (shorter):
Scanner input = new Scanner(new
File("mydata.txt"));
5
FILE PATHS AND DIRECTORIES
File Path: A description of a file’s location on a computer,
starting with a drive and including the path from the root
directory to the directory where the file is stored.
Current Directory: The directory that Java uses as the
default when a program uses a simple file name.
6
FILE PATHS AND DIRECTORIES
absolute path: specifies a drive or a top "/" folder
C:/Documents/smith/hw6/input/data.csv
Note: Windows can also use backslashes to separate folders.
relative path: does not specify any top-level folder
names.dat
input/kinglear.txt
Assumed to be relative to the current directory:
Scanner input = new Scanner(new
File("data/readme.txt"));
If our program is in
Scanner will look for
H:/hw6 ,
H:/hw6/data/readme.txt
7
COMPILER ERROR W/ FILES
import java.io.*;
import java.util.*;
// for File
// for Scanner
public class ReadFile {
public static void main(String[] args) {
Scanner input = new Scanner(new
File("data.txt"));
}
}
String text = input.next();
System.out.println(text);
The program fails to compile with the following error:
ReadFile.java:6: unreported exception
java.io.FileNotFoundException;
must be caught or declared to be thrown
Scanner input = new Scanner(new
File("data.txt"));
^
8
EXCEPTIONS
exception: An object representing a runtime error.
• dividing an integer by 0
• calling substring on a String and passing too large an index
• trying to read the wrong type of value from a Scanner
• trying to read a file that does not exist
We say that a program with an error "throws" an exception.
It is also possible to "catch" (handle or fix) an exception.
Checked Exception: An exception that must be caught or specifically
declared in the header of the method that might generate it.
We must specify how our program will handle file I/O failures.
9
THE THROWS CLAUSE
throws Clause: A declaration that a method will not attempt
to handle a particular type of exception.
Syntax:
public static type name(params)
throws ExceptionType {
Example:
public class ReadFile {
public static void main(String[] args)
throws FileNotFoundException {
Like saying, "I hereby announce that this method might throw an
exception, and I accept the consequences if this happens."
10
ACTIVITY TIME
Create a project call MadLib and have it open a story file
you create (look in your book at Programming Projects 5
for an example of the text for the story1.txt file)
Save this project as we’ll use it for the rest of this chapter,
as we’ll have the user enter in text for the placeholder
tokens.
Feel free to create additional story#.txt files to use later.
11
WHAT WE COVERED
• How to open a file in a directory you specify
• How to avoid a compiler error due to a FileNotFound
exception
12