Write a Java program called FindFile . The program will take two command line ar
ID: 3535745 • Letter: W
Question
Write a Java program called FindFile. The program will take two command line arguments.
Example usage: > java FindFile "C:/Program Files/Java" java.exe
The output will consist of a list of all files in all directories under the first command line argument that matches the second command line argument. The full path name will be displayed.
Remember to make your program robust so that if the user enters invalid command line arguments messages may be displayed (perhaps some exceptions thrown), but all situations should be handled reasonably gracefully (i.e., messages should explain the problem, and not just crash without any detailed indication of what is wrong).
Suggested references: Look at the java.io.File class in the Java Documentation. In particular, look at: listFiles(), getPath(), getName(), isDirectory()
Help please
Explanation / Answer
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
boolean bool = false;
try{
// create new files
f = new File("test.txt");
// create new file in the system
f.createNewFile();
// tests if file exists
bool = f.exists();
// prints
System.out.println("File exists: "+bool);
if(bool == true)
{
// delete() invoked
f.delete();
System.out.println("delete() invoked");
}
// tests if file exists
bool = f.exists();
// prints
System.out.print("File exists: "+bool);
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.