my problem is that I create a folder(name is IconResources) under src , in IconResources there are many pictures. Directory is like this:
- ProjectName
- src
- package 1
- package 2
- IconResources(this is the target folder)
- src
I want to list all picture files name and do something with those pictures. And I found that File.list() only works in IDE, if I export project to a jar to make a standalone, it cannot work. So I searched and found that I should use inputstream and outputstream. Now I find i/o stream can works well for single file. But I want to use inputstream and outputstream to read folder(IconResource), then list the files in that folder.
So my question is how to use i/o put stream to load a folder and iterate the folder to list file names in that folder. These things should work under not only IDE but also exported jar. My code are follows:
private void initalizeIconFiles(File projectDirectory){
URL a = editor.getClass().getResource("/IconResources/");// Get Folder URL
List<String> iconFileNames=new ArrayList<String>();//Create a list to store file names from IconResource Folder
File iconFolder = new File(a.getFile());
Path path=Paths.get(iconFolder.getPath());
DirectoryStream<Path> stream = Files.newDirectoryStream(path) ;
for (Path entry : stream) {
if (!Files.isDirectory(entry)) {
System.out.println(entry.getFileName().toString());
iconFileNames.add(entry.getFileName().toString());// add file name in to name list
}
}
}
above code only can run under IDE but break down in exported jar.