1 / 12

Building Java Programs Chapter 6

Building Java Programs Chapter 6. File Processing. Objectives!. Be able to open a file in a directory you specify Be able to avoid a compiler error due to a FileNotFound exception. Input/output (I/O).

Télécharger la présentation

Building Java Programs Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Building Java ProgramsChapter 6 File Processing

  2. Objectives! • Be able to open a file in a directory you specify • Be able to avoid a compiler error due to a FileNotFound exception

  3. 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");

  4. Methods of File Objects

  5. 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"));

  6. 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.

  7. 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 H:/hw6 ,Scanner will look for H:/hw6/data/readme.txt

  8. Compiler error w/ files import java.io.*; // for File import java.util.*; // 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")); ^

  9. 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.

  10. The throws clause throwsClause: A declaration that a method will not attempt to handle a particular type of exception. Syntax: public static typename(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."

  11. 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.

  12. What we covered • How to open a file in a directory you specify • How to avoid a compiler error due to a FileNotFound exception

More Related