Load all file in child directories
Hi!
I have problem. I must load to program all file(graphics) in folder, and in all child folder. I know how load, problem is load all images without having their names.
For example:
Quote:
Folder:
- image1.gif
- image2.gif
- image3.gif
- folder_child1:
- - image4.gif
- folder_child2:
- - image5.gif
- - image6.gif
- - child_folder_child2:
- - - image7.gif
- - - image8.gif
I mean, program loaded all image 1,2,3,4,5,6,7,8.
Number and folder names are unknown.
You know how can i resolve problem?
Greetings,
Rafał
PS: Sorry for my bad english.
Re: Load all file in child directories
Have a look at the File class; it can do what you want.
kind regards,
Jos
Re: Load all file in child directories
Ok, I make it.
But now I have this problem:
I need read only folders(directories) and graphist file(example only .gif).
You know, how make it?
Re: Load all file in child directories
You could use a FileFilter?
Or use the predone FileNameExtensionFIlter, and the isDirectory() method on File?
Re: Load all file in child directories
I use:
source.list(FilenameFilter.accept(source, ".gif"));
And it return error.
Re: Load all file in child directories
You don't fancy actually telling us the error, or do we have to guess?
Re: Load all file in child directories
It is example simple code:
Code:
import java.io.File;
import java.io.FilenameFilter;
public class lists {
public static void main(String[] args) {
String infile = "img/";
File source = new File(infile);
String[] files = source.list(FilenameFilter.accept(source, ".gif"));
for(int i=0;i<files.length;i++)
{
System.out.println(files[i]);
}
}
}
And theye are errors:
Code:
Cannot make a static reference to the non-static method accept(File, String) from the type FilenameFilter lists.java
The method list(FilenameFilter) in the type File is not applicable for the arguments (boolean)
Re: Load all file in child directories
You need to create a FilenameFilter object.
Re: Load all file in child directories
When I create a object:
Code:
FilenameFilter filter = new FilenameFilter();
It is error:
Quote:
Cannot intiantiate the type FilenameType
When I don't initialize:
Code:
import java.io.File;
import java.io.FilenameFilter;
public class lists {
public static void main(String[] args) {
String infile = "img/";
File source = new File(infile);
FilenameFilter filter;
String[] files = source.list(filter);
for(int i=0;i<files.length;i++)
{
System.out.println(files[i]);
}
}
}
It is error:
Quote:
The local variable filter may not have been initialized
Re: Load all file in child directories
Please give the actual errors, not your typed in interpretation of them.
FilenameFilter is an interface.
You have to write the code for it.