易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
如何使用Java合并多个文件
如何使用Java合并多个文件:
import
static
java.lang.System.out;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.nio.ByteBuffer;
import
java.nio.channels.FileChannel;
import
java.util.Arrays;
public
class
test {
public
static
final
int
BUFSIZE =
1024
*
8
;
public
static
void
mergeFiles(String outFile, String[] files) {
FileChannel outChannel =
null
;
out.println(
"Merge "
+ Arrays.toString(files) +
" into "
+ outFile);
try
{
outChannel =
new
FileOutputStream(outFile).getChannel();
for
(String f : files){
FileChannel fc =
new
FileInputStream(f).getChannel();
ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
while
(fc.read(bb) != -
1
){
bb.flip();
outChannel.write(bb);
bb.clear();
}
fc.close();
}
out.println(
"Merged!! "
);
}
catch
(IOException ioe) {
ioe.printStackTrace();
}
finally
{
try
{
if
(outChannel !=
null
) {outChannel.close();}}
catch
(IOException ignore) {}
}
}
public
static
void
main(String[] args) {
mergeFiles(
"D:/output.txt"
,
new
String[]{
"D:/in_1.txt"
,
"D:/in_2.txt"
,
"D:/in_3.txt"
});
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图