How do I iterate the directory contents and put each file into a List
or other collection?
thufir@dur:~/beanshell$
thufir@dur:~/beanshell$ bsh list_ebooks.bsh
ebook files to iterate
rw_ Dec 19 368225 1 - foundation - isaac asimov.epub
rw_ Dec 19 395042 2 - foundation and empire - isaac asimov.epub
rw_ Dec 19 374631 3 - second foundation - isaac asimov.epub
rw_ Dec 19 2084565 4 - foundation's edge - isaac asimov.epub
rw_ Dec 19 2125777 5 - foundation and earth - isaac asimov.epub
rw_ Dec 19 2187662 6 - prelude to foundation - isaac asimov.epub
rw_ Dec 19 2173565 7 - forward to foundation - isaac asimov.epub
thufir@dur:~/beanshell$
thufir@dur:~/beanshell$ cat list_ebooks.bsh
print("ebook files to iterate");
dir("/home/thufir/ebooks/foundation/");
thufir@dur:~/beanshell$
To bulk convert the format (although BeanShell
might be overkill).
BeanShell
will Load
the file? The fine manual:
Working with Dirctories and Paths BeanShell supports the notion of a current working directory for commands that work with files. The cd() command can be used to change the working directory and pwd() can be used to display the current value. The BeanShell current working directory is stored in the variable bsh.cwd.
All commands that work with files respect the working directory, including the following:
dir() source() run(), cat() load() save() mv() rm() addClassPath()
pathToFile() As a convenience for writing your own scripts and commands you can use the pathToFile() command to translate a relative file path to an absolute one relative to the current working directory. Absolute paths are unmodified.
absfilename = pathToFile( filename );
Import apache file utils?
It cannot be done with streams.
Assuming you have to avoid Streams you can go back to old java.io.File.listFiles()
method:
List<File> result = new ArrayList<>();
File dir = new File("/tmp");
File[] files = dir.listFiles();
if (files != null) {
for (File f : files) {
if (f.isFile()) {
result.add(f);
}
}
}
System.out.println(result);