Besides creating a file, the second most common operation performed on a file consists of opening one. You can open a file using the File class. As done previously, first declare a File variable and pass the name of the file to its constructor. Here is an example: import java.io.File; public class Exercise { public static void main(String[] args) throws Exception { // Incidate that you are planning to opena file File fleExample = new File("Example.xpl"); } }
To support the ability to read a value from a file, you can use the Scanner class. To support this operation, the Scanner class is equipped with a constructor that takes a File object as argument. Therefore, you can pass it a File variable you will have previously declared. Here is an example of declaring and initializing a variable for it: import java.io.File; import java.util.Scanner; public class Exercise { public static void main(String[] args) throws Exception { // Indicate that you are planning to opena file File fleExample = new File("Example.xpl"); // Prepare a Scanner that will "scan" the document Scanner opnScanner = new Scanner(fleExample); } } The values of a file are stored in or more lines. To continuously read the lines from the file, one at a time, you can use a while loop. In the while loop, continuously use the Scanner object that can read a line of value(s). In the while statement, to check whether the Scanner object has not gotten to the last line, you can check the status of its hasNext() method. As long as this method returns true, the Scanner reader has not gotten to the end of the file. Once the Scanner object has arrived to the end, this method would return false. Here is an example of implementing this scenario: import java.io.File; import java.util.Scanner; public class Exercise { public static void main(String[] args) throws Exception { // Indicate that you are planning to opena file File fleExample = new File("Example.xpl"); // Prepare a Scanner that will "scan" the document Scanner opnScanner = new Scanner(fleExample); // Read each line in the file while(opnScanner.hasNext()) { // Read each line and display its value System.out.println("First Name: " + opnScanner.nextLine()); System.out.println("Last Name: " + opnScanner.nextLine()); System.out.println("Hourly Salary: " + opnScanner.nextLine()); System.out.println("Is Full Time?: " + opnScanner.nextLine()); } } } After using the Scanner object, to free the resources it was using, call its close() method. Here is an example: import java.io.File; import java.util.Scanner; public class Exercise { public static void main(String[] args) throws Exception { // Indicate that you are planning to opena file File fleExample = new File("Example.xpl"); // Prepare a Scanner that will "scan" the document Scanner opnScanner = new Scanner(fleExample); // Read each line in the file while( opnScanner.hasNext() ) { // Read each line and display its value System.out.println("First Name: " + opnScanner.nextLine()); System.out.println("Last Name: " + opnScanner.nextLine()); System.out.println("Hourly Salary: " + opnScanner.nextLine()); System.out.println("Is Full Time?: " + opnScanner.nextLine()); } // De-allocate the memory that was used by the scanner opnScanner.close(); } }
Besides writing to a file or reading from it, there are many other actions you may to perform on a file. If you try opening a file that does not exist, you would receive an error: Exception in thread "main" java.io.FileNotFoundException: Example1.xpl (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.util.Scanner.<init>(Unknown Source) at Exercise.main(Exercise.java:9) Therefore, before opening a file, you may want to check first whether it exists. To assist you with this, the File class is equipped with the exists() method. Here is an example of calling it: import java.io.File; import java.util.Scanner; public class Exercise { public static void main(String[] args) throws Exception { // Indicate that you are planning to opena file File fleExample = new File("Example1.xpl"); // Find out if the file exists already if( fleExample.exists() ) { // Prepare a Scanner that will "scan" the document Scanner opnScanner = new Scanner(fleExample); // Read each line in the file while( opnScanner.hasNext() ) { // Read each line and display its value System.out.println("First Name: " + opnScanner.nextLine()); System.out.println("Last Name: " + opnScanner.nextLine()); System.out.println("Hourly Salary: " + opnScanner.nextLine()); System.out.println("Is Full Time?: " + opnScanner.nextLine()); } // De-allocate the memory that was used by the scanner opnScanner.close(); } else // if( !fleExample.exists() ) System.out.println("No file exists with that name"); } }
To delete a file, you can call the delete() method of the File class.
|
|
||||||||||||||||
|