我有一个包含多个文本文件的文件夹,我想将它们全部放入一个文本文件中。我想知道我是否能够使用文件阅读器,但我的代码无效。
public class All{
public static void main(String args[])throws IOException{
File file = new File("all.txt");
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write("alex/Desktop/1.txt");
writer.write("alex/Desktop/2.txt");
writer.flush();
writer.close();
FileReader fr = new FileReader(file);
char [] a = new char[50];
fr.read(a);
fr.close();
}
}
答案 0 :(得分:1)
使用Java 8,它非常简单,尽管以下内容使用one of my packages来解决可能的I / O错误:
final Path dstFile = Paths.get("your/destination/file");
final OutputStream out = Files.newOutputStream(dstFile);
final ThrowingConsumer<Path> copy = path -> Files.copy(path, out);
try (
final OutputStream outref = out;
) {
Stream.of("file1", "file2").map(Paths::get).forEach(copy);
}