|
File Existence
The following example shows how to check for the existence of a given file.
|
package com.bethecoder.tutorials.io;
import java.io.File;
public class CheckFileExistence {
/**
* @param args
*/
public static void main(String[] args) {
File file = new File("C:\\file_ops.txt");
if (file.exists()) {
System.out.println(file + " exists");
} else {
System.out.println(file + " doesn't exist");
}
file = new File("C:\\missing.txt");
if (file.exists()) {
System.out.println(file + " exists");
} else {
System.out.println(file + " doesn't exist");
}
}
}
|
| |
It gives the following output,
C:\file_ops.txt exists
C:\missing.txt doesn't exist
|
|