1 / 30

File IO

File IO. Includes Exceptions. Topics. General concepts Exception handling Creating a file in Eclipse Use of Scanner class Select a file (LineNumberer) Hard-code name Accept from keyboard File Chooser Command Line Exceptions (FileTotal) throws clause try/catch. Throw an exception

zagiri
Télécharger la présentation

File IO

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. File IO Includes Exceptions

  2. Topics • General concepts • Exception handling • Creating a file in Eclipse • Use of Scanner class • Select a file (LineNumberer) • Hard-code name • Accept from keyboard • File Chooser • Command Line • Exceptions (FileTotal) • throws clause • try/catch • Throw an exception • Create your own Exception (NegativeBalanceException) Be prepared for two quick exercises – start Eclipse now! Create a project named ExceptionsDemo

  3. Handling Errors try – prepare to execute statement that might yield an error error – exception is thrown error – exception is caught success – continue on You need to think about: • what statements might cause errors? • how to handle an error that occurs

  4. Prepare to read • FileReader is used to read data from file • Test to ensure you can find the file. • Wrap the FileReader in a Scanner object (pass the FileReader to the constructor) • Handle errors in data format. FileReader reader = new FileReader(“in.txt”); Scanner in = new Scanner(reader); Scanner String int double etc. “in.txt” FileReader FileReader

  5. Prepare for output • Use PrintWriter. (from documentation): • Print formatted representations of objects to a text-output stream. • Implements all of the print methods found in PrintStream. • Does not contain methods for writing raw bytes. • If automatic flushing is enabled it will be done only when one of the println() methods is invoked. • Methods include println (variety of parameters), write, close, flush, checkError (no exceptions thrown, must check for errors).

  6. Let’s get started – create a file • We’ll create a simple text file within Eclipse. • Choose File->New->Untitled Text (or just file) • Be sure to save in the project – not inside a package

  7. Line Numberer • Read an input file • Write the lines, with line numbers, to an output file

  8. LineNumberer – basic structure • We will need names for both the input and output files • We’ll see 4 different ways to do this. For now, set up variables. public class LineNumberer { private String inputName; private String outputName; public void setOutputName(String outputName) { this.outputName = outputName; } public void setInputName(String inputName) { this.inputName = inputName; } // rest of class }

  9. Number the lines public void numberLines() throws FileNotFoundException { FileReader reader = new FileReader(inputName); Scanner in = new Scanner(reader); PrintWriter out = new PrintWriter(outputName); int lineNumber = 1; while (in.hasNextLine()) { String line = in.nextLine(); out.println("/* " + lineNumber + " */ " + line); lineNumber++; } out.close(); } FileReader line “opens” the file What if files is not there? EXCEPTION! For now, we “acknowledge” with a throws clause

  10. Option 1: hardcode file names • In main LineNumberer lines1 = new LineNumberer(); lines1.setInputName("srcCode.txt"); lines1.setOutputName("srcCode1.out"); lines1.numberLines(); This is clearly not the most flexible option! Use sparingly!

  11. Option 2: prompt for names public void promptForInputFile() { Scanner scan = new Scanner(System.in); System.out.print("Enter the input file name: "); inputName = scan.next(); } • In main LineNumberer lines2 = new LineNumberer(); lines2.setOutputName("srcCode2.out"); lines2.promptForInputFile(); lines2.numberLines();

  12. Option 3: File Chooser • Can use javax.swing.JFileChooser. public void chooseFileName() { JFileChooserchooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { // static int File selectedFile = chooser.getSelectedFile(); inputName= selectedFile.getAbsolutePath(); } } • In main LineNumberer lines3 = new LineNumberer(); lines3.setOutputName("srcCode3.out"); lines3.chooseFileName(); lines3.numberLines();

  13. Option 4: Command line • Can accept filenames on the command line. May be easier to automate (e.g., put in a script) LineNumberer lines4 = new LineNumberer(); lines4.setOutputName("srcCode3.out"); if (args.length > 0) { lines4.setInputName(args[0]); lines4.numberLines(); }

  14. Eclipse New Run Configuration Select Run icon Press New Configuration Icon (be sure Java application is selected Fill in Name, Project and Main Class as needed

  15. Eclipse Arguments • Click on Arguments tag to add Program Arguments

  16. File Total • Program to calculate the sum of the values in a file • Handles situation if file not found • Handles situation if non-numeric value found

  17. FileTotal - Program Structure public class FileTotal { private int sum; private String inFile; public FileTotal(String inFile) { this.inFile = inFile; } public void displaySum() { System.out.println("Sum is " + sum); } public static void main(String[] args) { FileTotal ft = new FileTotal("numbers.txt"); ft.sumFileValues(); ft.displaySum(); } }

  18. The fun part public void sumFileValues() { FileReader reader = null; Scanner in = null; try{ reader = new FileReader(inFile); in = new Scanner(reader); } catch (FileNotFoundException e) { System.out.println (e.getLocalizedMessage()); } String strNum = null; try { while (in.hasNext()) { strNum = in.next(); int num = Integer.parseInt(strNum); sum += num; } } catch (NumberFormatException e) { System.out.println("Error, non-numeric value " + strNum); } }

  19. Quick Exercise – 5 minutes! • Create a Java Project called ExceptionsDemo • Create a class named ReadAFile • Create a method named loadFile • Write lines of code in loadFile to: • open a file named “numbers.txt” • read the first number • display on console • put the code in a try/catch block • display an error if file not found NOTE: names are just suggestions, this will not be turned in

  20. Software Engineering Quality Tip • Throw Early, Catch Late. Better to throw an exception than come up with an imperfect fix. What should program do if a file is not found? Is it always an error? • Do not squelch exceptions! try { FileReader reader = new FileReader(filename); … } catch (Exception e) {} // So there!

  21. Checked and Unchecked • Checked exceptions – can’t ignore, compiler requires that you handle or propagate • Unchecked exceptions – can be ignored. Generally subclass of RuntimeException, like NumberFormatException or NullPointerException. Too expensive to check all of these, often due to programmer error (checked exceptions often user error, like InputMismatchException) • If you aren’t going to handle a checked exception, put throws clause on function header: public void read(String filename) throws FileNotFoundException { . . .}

  22. Quick Exercise – 5 minutes • Remove the try/catch from your previous exercise. What error message do you see? • Put a “throws clause” on your loadFile method. What error do you see? • Put a “throws clause” on main. • Run the program. What happens? • Put the lines: Point p; system.out.println(p.getX()); in main. Does the compiler show an error? Would there be an error when you run the program?

  23. Exception Hierarchy Throwable Exception Error IOException RuntimeException ClassNot FoundException CloneNot Supported Exception ArithmeticException ClassCastException EOFException IllegalStateException FileNotFoundException NumberFormatException MalformedURLException IndexOutOfBoundsException UnknownHostException ArrayIndexOutOfBoundsException NoSuchElementException NullPointerException

  24. Finally Clause • Sometimes cleanup is needed even after an exception occurs PrintWriter out = new PrintWriter(filename); try { writeData(out); } finally { out.close(); // always executed, including after } // exception is handled in a catch

  25. Throwing an Exception public class ThrowException { public void getInput() throws Exception { Scanner scan = new Scanner(System.in); System.out.print("Enter a number from 1 - 100: "); int num = scan.nextInt(); if (num < 1 || num > 100) throw new Exception("The value " + num + " is not between 1 and 100!"); } public static void main(String[] args) throws Exception { ThrowException demo = new ThrowException(); try { demo.getInput(); } catch (Exception e) { System.out.println(e); System.out.println(e.getMessage()); } } } Is this a good use of exceptions? Probably not… if you can handle immediately with if/else logic, that’s better! But this example at least shows the mechanics of throwing an exception.

  26. Restricted File Total • Calculates the sum of values in file • Values must be greater than 0 NOTE: In reality, we might handle this with a simple if/else But this is a simple exercise to see how custom exceptions work Exception Best Practices are discussed in Programming Languages

  27. RestrictedFileTotal - Program Structure public class RestrictedFileTotal { private int sum; private String inFile; public RestrictedFileTotal(String inFile) { this.inFile = inFile; } public void displaySum() { System.out.println("Sum is " + sum); } }

  28. Designing your own Exception public class NegativeNumberException extends Exception { private int number; public NegativeNumberException() {} public NegativeNumberException(String message) { super(message); } public NegativeNumberException(int number) { super("Error: value can't be < 0, input = " + number); this.number = number; } public String toString() { return "Error: negative number encountered " + number; } } Could extend RuntimeException, if programmer can prevent Do we really need number? Probably not! Again, this is just to show the mechanics in a simple example.

  29. Throw your Exception public void sumFileValues() throws NegativeNumberException { FileReader reader = null; Scanner in = null; String strNum = null; try { reader = new FileReader(inFile); in = new Scanner(reader); while (in.hasNext()) { strNum = in.next(); int num = Integer.parseInt(strNum); if (num < 0) throw new NegativeNumberException(num); sum += num; } } catch (FileNotFoundException e) { System.out.println(e.getLocalizedMessage()); } catch (NumberFormatException e) { System.out.println("Error, non-numeric value “ + strNum); } }

  30. Catch your Exception public static void main(String[] args) { RestrictedFileTotalft = new RestrictedFileTotal("numbers.txt"); try { ft.sumFileValues(); } catch (NegativeNumberException e) { System.out.println(e); } ft.displaySum(); }

More Related