java 将文件夹中的所有文本文件合并为一个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17598153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combining all text files in a folder into a single file
提问by MxLDevs
How can I combine all txt files in a folder into a single file? A folder usually contains hundreds to thousands of txt files.
如何将文件夹中的所有txt文件合并为一个文件?一个文件夹通常包含数百到数千个 txt 文件。
If this program were only to be run on windows machines I would just go with a batch file containing something like
如果这个程序只在 Windows 机器上运行,我只会使用一个包含类似内容的批处理文件
copy /b *.txt merged.txt
copy /b *.txt merged.txt
But that is not the case, so I figured it might be easier to just write it in Java to complement everything else we have.
但事实并非如此,所以我认为用 Java 编写它来补充我们拥有的其他一切可能会更容易。
I have written something like this
我写过这样的东西
// Retrieves a list of files from the specified folder with the filter applied
File[] files = Utils.filterFiles(downloadFolder + folder, ".*\.txt");
try
{
// savePath is the path of the output file
FileOutputStream outFile = new FileOutputStream(savePath);
for (File file : files)
{
FileInputStream inFile = new FileInputStream(file);
Integer b = null;
while ((b = inFile.read()) != -1)
outFile.write(b);
inFile.close();
}
outFile.close();
}
catch (Exception e)
{
e.printStackTrace();
}
But it takes several minutes to combine thousands of files so it is not feasible.
但是合并数千个文件需要几分钟的时间,因此不可行。
采纳答案by Sotirios Delimanolis
Because of this
因为这
Integer b = null;
while ((b = inFile.read()) != -1)
outFile.write(b);
Your OS is making a lot of IO calls. read()
only reads one byte of data. Use the other read methodsthat accept a byte[]
. You can then use that byte[]
to write to your OutputStream
. Similarly write(int)
does an IO call writing a single byte. Change that too.
您的操作系统正在进行大量 IO 调用。read()
只读取一个字节的数据。使用其它读取方法接受一个byte[]
。然后,您可以使用它byte[]
来写入您的OutputStream
. 同样write(int)
做的IO调用写一个字节。也改一下。
Of course, you can look into tools that do this for you, like Apache Commons IO or even the Java 7 NIO package.
当然,您可以查看为您执行此操作的工具,例如 Apache Commons IO 甚至 Java 7 NIO 包。
回答by fge
Use NIO, it is mucheasier than using inputstreams/outputstreams. Note: uses Guava's Closer
, which means all resources are safely closed; even better would be to use Java 7 and try-with-resources.
使用NIO,它是很多比使用inputstreams / outputstreams容易。注意:使用 Guava 的Closer
,表示所有资源都安全关闭;更好的是使用 Java 7 和 try-with-resources。
final Closer closer = Closer.create();
final RandomAccessFile outFile;
final FileChannel outChannel;
try {
outFile = closer.register(new RandomAccessFile(dstFile, "rw"));
outChannel = closer.register(outFile.getChannel());
for (final File file: filesToCopy)
doWrite(outChannel, file);
} finally {
closer.close();
}
// doWrite method
private static void doWrite(final WriteableByteChannel channel, final File file)
throws IOException
{
final Closer closer = Closer.create();
final RandomAccessFile inFile;
final FileChannel inChannel;
try {
inFile = closer.register(new RandomAccessFile(file, "r"));
inChannel = closer.register(inFile.getChannel());
inChannel.transferTo(0, inChannel.size(), channel);
} finally {
closer.close();
}
}
回答by Jean Logeart
Try using BufferedReaderand BufferedWriterinstead of writing bytes one by one.
尝试使用BufferedReader和BufferedWriter而不是一一写入字节。
回答by kiran
You can use IoUtils to merge files,IoUtils.copy() method will help you for merging files.
您可以使用 IoUtils 来合并文件,IoUtils.copy() 方法将帮助您合并文件。
This link may be useful merging file in java
此链接可能对在 Java 中合并文件很有用
回答by Zafer
I would do it this way !
我会这样做!
check for the OS
System.getProperty("os.name")
Run the System Level command from Java.
If windows
copy /b *.txt merged.txt
if Unix
cat *.txt > merged.txt
or whatever best System level command available.
检查操作系统
System.getProperty("os.name")
从 Java 运行系统级命令。
如果窗户
copy /b *.txt merged.txt
如果是 Unix
cat *.txt > merged.txt
或任何可用的最佳系统级命令。