You could do it like that:
File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();
if(listOfFiles != null) {
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
}
Do you want to only get JPEG files or all files?
Answer from RoflcoptrException on Stack OverflowYou could do it like that:
File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();
if(listOfFiles != null) {
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
}
Do you want to only get JPEG files or all files?
Create a File object, passing the directory path to the constructor. Use the listFiles() to retrieve an array of File objects for each file in the directory, and then call the getName() method to get the filename.
List<String> results = new ArrayList<String>();
File[] files = new File("/path/to/the/directory").listFiles();
//If this pathname does not denote a directory, then listFiles() returns null.
for (File file : files) {
if (file.isFile()) {
results.add(file.getName());
}
}
Videos
My project requires the user to enter a number of files to be loaded into a search object but I was worried the user would not know the files that are allowed to be uploaded. I have several in my src folder to use and I've figured out how to use the path of the files on my wn computer, but when I zip this project up and submit it obviously my professor is not going to have the src folder in the same location. How do I specify this location in relation to the project itself? My code right now looks like this:
File[] files = new File("C:\\Users\\User\\eclipse-workspace\\test").listFiles();And this is exactly what I want it to do I just don't know how I can get this to work everytime since the project Im working on will be saved elsewhere. I'm kind of a novice please go easy on me
Thanks for all your help! I found my problem, i was reading the path from a configuration file which i assumed to be in the user's home (my home and System's home are not the same... my fault). Unfourtenly i didn't realize that because i was using a recycled class (:S). Again Thank you very much.
Based on the documentation for File.isDirectory(), it sounds like you may have a security manager enabled. isDirectory requires the checkRead permission to be enabled, and you can test this yourself by using the something like the following (edit as needed):
SecurityManager security = System.getSecurityManager();
if(security)
if(security.checkRead(FILE_PATH_HERE))
// we have permission
else
// no permission
Adding the directory to the list of allowed directories may or may not be on your list of things to do. There are dozens of Google links with instructions if this is the case. The same goes if you would like to disable the SM completely.