如何将迭代目录内容并将每个文件放在转换为List
或其他集合?
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$
若要散装转换格式,请执行以下操作(尽管BeanShell
可能有些过分)。
BeanShell
会Load
文件吗?The 精细手册
BeanShell支持为处理文件的命令提供当前工作目录的概念。cd()命令可用于更改工作目录,pwd()可用于显示当前值。BeanShell当前工作目录存储在变量bsh.cwd中。 所有与文件一起工作的命令都尊重工作目录,包括以下内容: dir() source() run(),cat() load() save() mv() rm() addClassPath() 为了编写自己的脚本和命令,pathToFile()可以使用pathToFile()命令将相对文件路径转换为相对于当前工作目录的绝对路径。绝对路径未经修改。
absfilename = pathToFile( filename );
导入 apache文件实用程序?
这不能用流来完成。
发布于 2019-01-02 17:54:00
假设您必须避免流,您可以返回到旧的java.io.File.listFiles()
方法:
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);
https://stackoverflow.com/questions/53869099
复制相似问题