在目录中的多个文件上运行Java程序,输出具有唯一名称 [英] run a java program on multiple files in a directory, output with unique names
问题描述
我的目录结构形式为:
base_directory
/ level_one_a
, level_one_b
, level_one_c
/
然后在 level_one_x
中的所有这些目录中是多个后续目录,即
then within all those directories in level_one_x
are a multitude of subsequent directories, i.e.
/ level_one_a_1
, level_one_a_2
, level_one_a_3
...
等等,以 level_one_b
& level_one_c
and so on for level_one_b
& level_one_c
然后在 level_one_a_1
内,即 level_one_a_1_I
, level_one_a_1_II
, level_one_a_1_III
, level_one_a_1_IV
...
then inside of level_one_a_1
we have more still, i.e. level_one_a_1_I
,level_one_a_1_II
,level_one_a_1_III
,level_one_a_1_IV
...
然后最后在 level_one_a_1_IV
里面,以及所有处于同一级别的文件,都是我要操作的文件。
Then finally inside of level_one_a_1_IV
, and all those on the same level, are the files I want to operate on.
我想用一种简短的方式来表示,即 start
/ 一个
/ 两个
/ 三个
/ * files *
I guess a shorter way to say that would be start
/one
/two
/three
/*files*
文件很多,并且我想用我编写的简单Java程序来处理它们:
There are many many files and I want to process them all with a simple java program I wrote:
try
{
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null)
{
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
Document doc = Jsoup.parse(everything);
String link = doc.select("block.full_text").text();
System.out.println(link);
}
finally
{
br.close();
}
它使用jsoup
我想构建此脚本,以便程序可以自动导航此目录结构并抓取每个文件,然后使用缓冲读取器和文件读取器使用该脚本处理该文件,我想,我该怎么办?方便吗?我尝试实施此解决方案,但我无法不能正常工作。
I'd like to construct this script such that the program can navigate this directory structure autonomously and grab each file then process it with that script, using buffered reader and file reader I guess, how can I facilitate that? I tried implementing this solution but I couldn't get it to work.
理想情况下,我想使用唯一名称输出其处理的每个文件,即该文件名为 00001.txt
可能会将其另存为 00001_output.txt
,但这是另一种颜色的马
Ideally I want to output each file it processes with a unique name, i.e. is the file is named 00001.txt
it might save it as 00001_output.txt
but, that's a horse of a different colour
推荐答案
只需使用 java.io.File
及其方法 listFiles
。
请参见javadoc 文件API
Just use java.io.File
and its method listFiles
.
See javadoc File API
关于SO的类似问题发布在这里:
递归列出Java中的文件
Similar question on SO was posted here: Recursively list files in Java
这篇关于在目录中的多个文件上运行Java程序,输出具有唯一名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!