Credo che intanto ti debba studiare la classe File e i suoi metodi tanto utili.
Ti mando un po di codice d'esempio:
Es 1:
codice:
import java.io.File;
/**
*Questo esempio verifica l'esistenza di un file
* @author mau2
*/
public class TryFile {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Creazione di un oggetto che e' una directory
String parent = new File(System.getProperty("java.home")).getParent();
File myDir = new File(parent,
File.separator + "src" + File.separator + "java" + File.separator + "io");
System.out.println(myDir + (myDir.isDirectory() ? " is" : " is not") +
" a directory");
//creazione di un oggetto che e' un file
File myFile = new File(myDir, "File.java");
System.out.println(myFile + (myFile.exists() ? " does" : " does not") +
" exist");
System.out.println(myFile + (myFile.isFile() ? " is" : " is not") +
" a file");
System.out.println(myFile + (myFile.isHidden() ? " is" : " is not") +
" hidden");
System.out.println("You can" + (myFile.canRead() ? " " : "not ") +
"read " + myFile);
System.out.println("You can" + (myFile.canWrite() ? " " : "not ") +
"write " + myFile);
System.out.println( );
}
}
es2 :
codice:
public class TryFile2 {
/**Ottieni ulteriori informazioni
* @param args the command line arguments
*/
public static void main(String[] args) {
// Creazione di un oggetto che e' una directory
String parent = new File(System.getProperty("java.home")).getParent();
File myDir = new File(parent,
"src" + File.separator + "java" + File.separator + "io");
System.out.println(myDir.getAbsolutePath()
+ (myDir.isDirectory() ? " is " : " is not ")
+ "a directory");
System.out.println("The parent of " + myDir.getName() + " is "
+ myDir.getParent());
// Get the contents of the directory
File[] contents = myDir.listFiles();
// List the contents of the directory
if (contents != null) {
System.out.println("\nThe " + contents.length
+ " items in the directory " + myDir.getName()
+ " are:");
for (int i = 0; i < contents.length; i++) {
System.out
.println(contents[i] + " is a "
+ (contents[i].isDirectory() ? "directory" : "file")
+ " last modified "
+ new Date(contents[i].lastModified()));
}
} else {
System.out.println(myDir.getName() + " is not a directory");
}
}
}