在Java中连接多个.txt文件
我有许多在Java中连接多个.txt文件,java,concatenation,Java,Concatenation,我有许多.txt文件。我想将它们连接起来并生成一个文本文件。 我将如何在Java中实现它 情况如下 将结果串联到 file3.txt 这样file1.txt的内容后面跟着file2.txt听起来像是家庭作业 打开文件1 打开文件2 创建/打开文件3 读取文件1并写入文件3 关闭文件1 读取文件2并写入文件3 关闭文件2 关闭文件3 如果您需要知道如何在Java中创建/打开/读取/写入/关闭文件,请搜索文档。应该可以广泛使用。您的意思是您需要一个包含其他文本文件内容的文件?然后,读取每个文件(
.txt
文件。我想将它们连接起来并生成一个文本文件。我将如何在Java中实现它
情况如下 将结果串联到
file3.txt
这样
file1.txt
的内容后面跟着file2.txt
听起来像是家庭作业
如果您需要知道如何在Java中创建/打开/读取/写入/关闭文件,请搜索文档。应该可以广泛使用。您的意思是您需要一个包含其他文本文件内容的文件?然后,读取每个文件(可以循环执行),将其内容保存在StringBuffer/ArrayList中,并通过将StringBuffer/ArrayList中保存的文本刷新到最终的.txt文件来生成最终的.txt文件
别担心,这是一项容易的任务。只要习惯给定的系统,就可以了:)逐个文件读取文件并将它们写入目标文件。如下所示:
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[n];
for (String file : files) {
InputStream in = new FileInputStream(file);
int b = 0;
while ( (b = in.read(buf)) >= 0)
out.write(buf, 0, b);
in.close();
}
out.close();
这对我来说很好
// open file input stream to the first file file2.txt
InputStream in = new FileInputStream("file1.txt");
byte[] buffer = new byte[1 << 20]; // loads 1 MB of the file
// open file output stream to which files will be concatenated.
OutputStream os = new FileOutputStream(new File("file3.txt"), true);
int count;
// read entire file1.txt and write it to file3.txt
while ((count = in.read(buffer)) != -1) {
os.write(buffer, 0, count);
os.flush();
}
in.close();
// open file input stream to the second file, file2.txt
in = new FileInputStream("file2.txt");
// read entire file2.txt and write it to file3.txt
while ((count = in.read(buffer)) != -1) {
os.write(buffer, 0, count);
os.flush();
}
in.close();
os.close();
//打开第一个文件file2.txt的文件输入流
InputStream in=新文件InputStream(“file1.txt”);
byte[]buffer=使用Apache Commons IO的新字节[1]
你可以用图书馆。这有一个班级
此类中还有其他方法可以帮助以更优化的方式完成任务(例如使用流或列表)
使用Java7+
如果您使用的是Java7+
public static void main(String[] args) throws Exception {
// Input files
List<Path> inputs = Arrays.asList(
Paths.get("file1.txt"),
Paths.get("file2.txt")
);
// Output file
Path output = Paths.get("file3.txt");
// Charset for read and write
Charset charset = StandardCharsets.UTF_8;
// Join files (lines)
for (Path path : inputs) {
List<String> lines = Files.readAllLines(path, charset);
Files.write(output, lines, charset, StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
}
}
publicstaticvoidmain(字符串[]args)引发异常{
//输入文件
列表输入=Arrays.asList(
path.get(“file1.txt”),
get(“file2.txt”)
);
//输出文件
路径输出=Path.get(“file3.txt”);
//读写字符集
Charset Charset=StandardCharsets.UTF_8;
//连接文件(行)
用于(路径:输入){
列表行=文件.readAllLines(路径,字符集);
写入(输出、行、字符集、StandardOpenOption.CREATE、,
标准OpenOption.APPEND);
}
}
是的,我曾经在unix命令中使用cat来执行此操作。但我希望在我的程序中执行此操作。@casperOne这是一个非常真实的问题,有几个人已经理解了它,并提供了有用的信息,例如指向FileUtils类的指针。是的,肯定是一个真实的问题。我确切地询问了我想知道的内容,下面的答案很有帮助。+1.它这是一个非常合理的问题,因为纯Java的最短答案是11行代码。与UNIX shell中的#cat file1 file2>file3相比。描述了使用通道而不是将所有内容读取到内存中的性能显著提高。您还可以使用Guava的ByteSource.concat获得所有内容的输入流,然后传递它可以扩展到FileChannel.transferToI中,因为它可以扩展到两个以上的文件,并且不需要在内存中保存太多的内容。@alexr你能不能再扩展一点并逐步解释它?外循环在文件上迭代。内循环使用n字节长的缓冲区读取文件内容。只要看看java st的任何教程就可以了详细信息请阅读。你能一步一步地解释你的代码吗?@joelbonter我添加了一些注释来解释这段旧代码。不过,我认为上面的一些答案更好。
// Files to read
File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
// File to write
File file3 = new File("file3.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 for append
public static void main(String[] args) throws Exception {
// Input files
List<Path> inputs = Arrays.asList(
Paths.get("file1.txt"),
Paths.get("file2.txt")
);
// Output file
Path output = Paths.get("file3.txt");
// Charset for read and write
Charset charset = StandardCharsets.UTF_8;
// Join files (lines)
for (Path path : inputs) {
List<String> lines = Files.readAllLines(path, charset);
Files.write(output, lines, charset, StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
}
}