term1 Definition1term2 Definition2term3 Definition3
Please sign in to your Google account to access your documents:
Exception Handling
different ways allow the caller to deal with the exceptions that occur within the program instead of allowing the methods to deal with it (which usually involves completely terminating the program).
a throw new statement allows the method to throw an exception if that exception is found, and if not the code runs normally.
the try block contains the code that would run under normal circumstances, while the catch block is the portion where it deals with the exception if there is one after running the try block.
Syntax:
try {
code to run;
a statement or method that may throw an exception;
more code to run;
}
catch(type ex) {
code to express the exception
Recursion
the idea of calling one function from another immediately suggests the possibility of a function calling itself. the function-call mechanism in Java supports this possibility, known as recursion.
Files
The File class contains methods for obtaining the properties of a file/directory and for renaming and deleting a file/directory.
The Scanner allows us to read the information inside the file, while the PrintWriter allows us to create a new file and write data to the text file.
Scanner
import java.util.Scanner;
Scanner input = new Scanner (new File(filename));
EXAMPLE
Scanner input = new Scanner (System.in);
Scanner output = null; //this reads the file
boolean x = false;
String fileName;
while (x) {
System.our.println("Enter the file's name: ");
fileName = input.next(); //first find your file
try{
File fileN = new File(fileN ".txt");
output = new Scanner(fileN);
boolean existsFile = fileN.exists();
if(existFile == true){
System.out.println("File" + fileName + "successfully opened!");
x = true;
catch(FileNotFoundException ex){
System.out.println("FileNotFoundEception: the file cannot be found");
Now you can read the file by simply doing:
input.nextLine(); //reads one complete line of the file, an empty space if there is one
PrintWriter
-creates a new file with written information in it
IO: stands for input/output
import java.io.PrintWriter;
PrintWriter output = new PrintWriter (filename);
Example:
System.out.println("Enter the file's name: ");
String fileName = input.next();
File files = new File(fileName + ".txt");
Scanner x = new Scanner(files);
PrintWriter overwrite = nulll;
String name = x.nextLine();
String color = x.nextLine(); //etc...
//then use PrintWriter to create a new file based on the read information and write information on it
overwrite = new PrintWriter(filename + "_revised.txt"); //the new file created
overwrite.println("Files inside of here...");
//what is being written inside of file
Need help typing ? See our FAQ (opens in new window)
Please sign in to create this set. We'll bring you back here when you are done.
Discard Changes Sign in
Please sign in to add to folders.
Sign in
Don't have an account? Sign Up »
You have created 2 folders. Please upgrade to Cram Premium to create hundreds of folders!