所以我一直试图弄清楚如何实现这一目标。我有一个名为" part1.txt"它出现在多个目录中。例如,它出现在目录中:
usr/documents/trial/part1.txt
usr/documents/trial2/part1.txt
usr/documents/zip/part1.txt
这些part1.txt文件中的每一个都包含不同类型的信息。我想编写一个java程序将所有这些文件合并到一个文件中。有没有办法在java中实现这一点?或者我应该使用hadoop来执行这样的任务?如果有人能告诉我如何编写这个程序会很棒。
答案 0 :(得分:1)
首先看一下Basic I/O
基本上,您需要对要包含的目录进行递归搜索,并将内容附加到其他文件
例如,您可以简单地创建一个BufferedWriter
,它允许您将内容写入a,在本例中为特定文件(或主文件)......
public static void main(String[] args) {
File output = new File("Master-Part1.txt");
try (BufferedWriter bw = new BufferedWriter(new FileWriter(output))) {
findAndAppend(new File("."), bw);
} catch (IOException exp) {
exp.printStackTrace();
}
}
您需要扫描特定文件以查找任何匹配项(part1.txt
),并将其内容附加到BufferedWriter
(如果找到)。
完成当前目录后,您需要尝试扫描子目录,如果有的话......
public static void findAndAppend(File parent, BufferedWriter bw) throws IOException {
// Find any matching files...
File files[] = parent.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().equals("part1.txt");
}
});
// Append any results...technically there should only be 0-1
// matches, but this is a nice example ;)
for (File file : files) {
append(file, bw);
}
// Find the sub directories...
File dirs[] = parent.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
// Scan the sub directories...
for (File dir : dirs) {
findAndAppend(dir, bw);
}
}
最后,您需要能够将任何匹配的内容写入主文件...
protected static void append(File file, BufferedWriter bw) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String text = null;
while ((text = br.readLine()) != null) {
bw.write(text);
bw.newLine();
}
} finally {
}
}
此示例使用Java 7的try-with-resources功能,因此请确保您运行的是Java 7.
请查看java.io.File
了解详情
更新了walkFileTree
示例(Java 7)
public static void main(String[] args) {
File master = new File("Master-part1.txt");
try (BufferedWriter bw = new BufferedWriter(new FileWriter(master))) {
Path path = master.toPath();
Files.walkFileTree(path, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toFile().getName().equals("part1.txt")) {
append(file.toFile(), bw);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException exp) {
exp.printStackTrace();
}
}
protected static void append(File file, BufferedWriter bw) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String text = null;
while ((text = br.readLine()) != null) {
bw.write(text);
bw.newLine();
}
} finally {
}
}
答案 1 :(得分:1)
您可以查看以下内容:
public File mergeFiles(List<File> files,
String mergedFileName) {
File mergedFile = new File(mergedFileName);
BufferedWriter bufferedWriter = null;
try {
FileWriter fileWriter = new FileWriter(mergedFile, true);
bufferedWriter = new BufferedWriter(fileWriter);
} catch (IOException e) {
e.printStackTrace();
}
for (File f : files) {
FileInputStream fis;
try {
fis = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String aLine;
while ((aLine = in.readLine()) != null) {
bufferedWriter.write(aLine);
bufferedWriter.newLine();
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
return mergedFile;
}
答案 2 :(得分:0)
有很多方法可以做到这一点。您需要知道或搜索文本文件的所有版本以及您提供的名称,然后将每个版本读入程序,然后使用stringbuilder创建单个字符串并将其写回文件。需要考虑的是,如果合并的顺序很重要,并且您还想删除您正在抓取的旧文件。
答案 3 :(得分:0)
您似乎正在使用Unix / Linux系统。如果您不受Java限制,请查看cat
命令。否则,以下内容应该有效(使用Apache commons):
// Files to read
File file1 = new File("usr/documents/trial/part1.txt");
File file2 = new File("usr/documents/trial2/part1.txt");
// File to write
File file3 = new File("concatenated-file.txt");
// Read the file as string
String file1Str = FileUtils.readFileToString(file1);
String file2Str = FileUtils.readFileToString(file2);
// Write the file
FileUtils.write(file3, file1Str);
FileUtils.write(file3, file2Str, true); // true => append mode
答案 4 :(得分:0)
我测试了非常大的文件,它工作正常!
public static void mergeFiles(List<File> files, File target) {
OutputStream fos = null;
try {
fos = new FileOutputStream(target);
} catch (IOException e) {
e.printStackTrace();
}
for (File f : files) {
InputStream fis = null;
try {
fis = new FileInputStream(f);
byte[] buf = new byte[4096];
int i;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}