Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS0007: Introduction to Computer Programming File IO and Recursion.

Similar presentations

Presentation on theme: "CS0007: Introduction to Computer Programming File IO and Recursion."— Presentation transcript:

1 CS0007: Introduction to Computer Programming File IO and Recursion

2 Review The steps taken when reading or writing to a file: Open the file. Data is written or read from a file. When the program is finished with the file, the file must be closed. Two kinds of files in input and output: Input File – File in which data is read from. Output File – File in which data is written to. The Java API provides a class for writing to a file called PrintWriter. The parameter supplied to the PrintWriter constructor is… A filename

3 Review Two methods used to write to a file: print println Two classes needed to read from a file: File Scanner Method that reads a single line from a file: readLine Method that detects if there is anything left in a file: hasNext

4 Appending to a File When you create a PrintWriter object by passing the file’s name as an argument to the constructor, it will erase the contents of the file if it exists. What can I do if I want to retain the text already in the file and add to the end of it? Answer: You can append to the file Appending means adding to the end. You can append text to the end of a file by creating a FileWriter object and passing it to the PrintWriter ’s constructor: FileWriter fwriter = new FileWriter("names.txt", true); PrintWriter outputFile = new PrintWriter(fwriter); Notes: FileWriter throws an IOException, if you do the same steps for handling the FileNotFoundException, but with IOException instead, you can handle both the FileNotFoundException and the IOException. The second argument for the FileWriter’s constructor indicates that you want to append to the file Example:

5 Specifying the File Path When you give a file’s name as an argument, you can specify the entire path to the file if you want it to be read or written to a speciffic location. For instance, if the path to a file on my desktop on the windows machine in my office would be: \\\Users\Users1\eth13\Desktop\myFile.txt If I had to supply this as a parameter, what would be the problem? oAnswer: Windows uses backslashes as file separators. Strings in Java uses backslashes to begin escape sequences. oSolution: Java gives you the ability to replace all backslashes with forward slashes. // Example:

6 Detecting Whether a File Exists The File class has a method called exists that checks to see if the file specified by the path already exists. This can be used in both reading and writing to a file: In reading, instead of having your program end abnormally with a runtime error, you can display an error message. Example: In writing, you can warn the user if she is about to overwrite an existing file. Example:

7 Recursion Recursion is the process of repeating something in a self-similar way. The way this appears in programming is recursive methods. A recursive method is a method that calls itself. What is the problem with this: This method has no way of stopping the calls to itself, so it runs forever! oWhat it needs is a base case. A base case is a condition in which the method stops calling itself. So a recursive method has two parts: A base case. A recursive case (A case in which it calls itself) Example: Usually, the number of recursive calls (depth of recursion) is dependent on an argument passed to a method. Each time a method is called, a new copy of the parameter is made for that method call.

8 Recursion Recursion is confusing…why does anyone use it? Good Question! The fact of the matter is that all recursive problems can be solved iteratively (with loops), and iterative solutions are often more efficient than recursive ones. Answer: Some problems are easily defined a solved recursively. If a problem can be broken down into successive smaller parts that are identical to the original problem, it is often easy to think of their solutions as recursive ones. oFor example, consider factorial.factorial In general, a recursive method works like this: If the problem can be solved now, without recursion, then the method solves it and returns the value. If the problem, cannot be solved now, then the method reduces it to a smaller but similar problem ands calls itself to solve the smaller problem. The way you design a recursive method is to identify the recursive and base cases. Now you try: Many problems can be solved recursively: Fibonacci Numbers Towers of Hanoi

Download ppt "CS0007: Introduction to Computer Programming File IO and Recursion."

Similar presentations

Ads by Google