CS0007: Introduction to Computer Programming File IO and Recursion.

Click here to load reader

download CS0007: Introduction to Computer Programming File IO and Recursion.

of 8

Transcript of CS0007: Introduction to Computer Programming File IO and Recursion.

PowerPoint Presentation

CS0007: Introduction to Computer ProgrammingFile IO and Recursion1ReviewThe 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 calledPrintWriter.The parameter supplied to the PrintWriter constructor isA filename

ReviewTwo methods used to write to a file:printprintlnTwo classes needed to read from a file:FileScannerMethod that reads a single line from a file:readLineMethod that detects if there is anything left in a file:hasNext

Appending to a FileWhen you create a PrintWriter object by passing the files 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 fileAppending 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 PrintWriters 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 FileWriters constructor indicates that you want to append to the fileExample: Append.javaSpecifying the File PathWhen you give a files 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:\\ad.cs.pitt.edu\Users\Users1\eth13\Desktop\myFile.txt If I had to supply this as a parameter, what would be the problem?Answer: Windows uses backslashes as file separators. Strings in Java uses backslashes to begin escape sequences.Solution: Java gives you the ability to replace all backslashes with forward slashes.//ad.cs.pitt.edu/Users/Users1/eth13/Desktop/myFile.txtExample: FilePath.java

Detecting Whether a File ExistsThe 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: ReadExists.javaIn writing, you can warn the user if she is about to overwrite an existing file.Example: WriteExists.javaRecursionRecursion 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: BadRecursion.java?This method has no way of stopping the calls to itself, so it runs forever!What 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: GoodRecursion.javaUsually, 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. RecursionRecursion is confusingwhy 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.For example, consider 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: Factorial.javaMany problems can be solved recursively:Fibonacci NumbersFibonacci.javaTowers of HanoiHanoi.javaHanoiDemo.java