Ⅰ 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"});
}
}
Ⅱ java 怎么样把多个Excel 合并为一个
程序中引用spire.xls.jar包
import com.spire.xls.*;
public class MergeExcels {
public static void main(String[] args){
//将待合并Excel文档的名称保存至字符串数组
String[] inputFiles = new String[]{"file1.xlsx","file2.xlsx"};
//创建一个新的Excel文档
Workbook newBook = new Workbook();
//清除默认的3张工作表
newBook.getWorksheets().clear();
//创建另一个Excel文档
Workbook tempBook = new Workbook();
//遍历数组,依次加载每个Excel文档并将文档中的所有工作表复制到新建的Excel文档中
for (String file : inputFiles)
{
tempBook.loadFromFile(file);
for (Worksheet sheet : (Iterable)tempBook.getWorksheets())
{
newBook.getWorksheets().addCopy(sheet, WorksheetCopyType.CopyAll);
}
}
//保存
newBook.saveToFile("MergeFiles.xlsx", ExcelVersion.Version2013);
}
}
Ⅲ 如何使用java合并多个文件
使用java编程语言,对文件进行操作,合并多个文件,代码如下:
importstaticjava.lang.System.out;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.nio.ByteBuffer;
importjava.nio.channels.FileChannel;
importjava.util.Arrays;
publicclasstest{
publicstaticfinalintBUFSIZE=1024*8;
publicstaticvoidmergeFiles(StringoutFile,String[]files){
FileChanneloutChannel=null;
out.println("Merge"+Arrays.toString(files)+"into"+outFile);
try{
outChannel=newFileOutputStream(outFile).getChannel();
for(Stringf:files){
FileChannelfc=newFileInputStream(f).getChannel();
ByteBufferbb=ByteBuffer.allocate(BUFSIZE);
while(fc.read(bb)!=-1){
bb.flip();
outChannel.write(bb);
bb.clear();
}
fc.close();
}
out.println("Merged!!");
}catch(IOExceptionioe){
ioe.printStackTrace();
}finally{
try{if(outChannel!=null){outChannel.close();}}catch(IOExceptionignore){}
}
}
//下面代码是将D盘的1.txt2.txt3.txt文件合并成out.txt文件。
publicstaticvoidmain(String[]args){
mergeFiles("D:/output.txt",newString[]{"D:/1.txt","D:/2.txt","D:/3.txt"});
}
}
Ⅳ 用java实现合并多个txt文件
可以试下用BufferedReader和BufferedWriter进行文件读写,合并在读的时候做就行了。
Ⅳ java如何合并多个大的txt文件(每个文件50M)。nio处理文件如何提高速度
这种情况java.io, nio没有大区别
byte[]buf=newbyte[8*1024];
try(OutputStreamout=newnewFileOutputStream(outfile)){
for(Filef:txtFiles){
try(FileInputStreamin=newFileInputStream(f)){
org.apache.commons.io.IOUtils.Large(in,out,buf);
}
}
}
要是linux下,shell里直接执行cat *.txt >out.txt就可以,不用写代码
Ⅵ 用java io流把多个txt文件的内容合并到一个文件里
参考代码如下:
public static void mergeFiles(String outFile, String[] files)
第一个参数是合并后生成文件的路径
第二个参数是你需要合并的文本文件列表
代码:
packageorg.lq.util;
importstaticjava.lang.System.out;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.nio.ByteBuffer;
importjava.nio.CharBuffer;
importjava.nio.channels.FileChannel;
importjava.nio.charset.Charset;
importjava.nio.charset.CharsetDecoder;
importjava.nio.charset.CharsetEncoder;
importjava.util.Arrays;
publicclassMergeFile{
publicstaticfinalintBUFSIZE=1024*8;
publicstaticvoidmergeFiles(StringoutFile,String[]files){
FileChanneloutChannel=null;
out.println("Merge"+Arrays.toString(files)+"into"+outFile);
try{
outChannel=newFileOutputStream(outFile).getChannel();
for(Stringf:files){
Charsetcharset=Charset.forName("utf-8");
CharsetDecoderchdecoder=charset.newDecoder();
CharsetEncoderchencoder=charset.newEncoder();
FileChannelfc=newFileInputStream(f).getChannel();
ByteBufferbb=ByteBuffer.allocate(BUFSIZE);
CharBuffercharBuffer=chdecoder.decode(bb);
ByteBuffernbuBuffer=chencoder.encode(charBuffer);
while(fc.read(nbuBuffer)!=-1){
bb.flip();
nbuBuffer.flip();
outChannel.write(nbuBuffer);
bb.clear();
nbuBuffer.clear();
}
fc.close();
}
out.println("Merged!!");
}catch(IOExceptionioe){
ioe.printStackTrace();
}finally{
try{if(outChannel!=null){outChannel.close();}}catch(IOExceptionignore){}
}
}
}
Ⅶ java合并两个txt文件并生成新txt
importjava.io.File;
importjava.io.PrintStream;
importjava.util.Scanner;
/**
*2015年11月18日上午9:31:05
*
*@authorcs2110TODO合并数组
*
*/
publicclassMergeFile{
privateStringafile="D:/1.txt";
privateStringbfile="D:/2.txt";
privateStringmergefile="D:/3.txt";
/**
*读取文件里面的整数
*
*@paraminput
*Scanner对象
*@return返回整形数组
*/
publicint[]readFile(Scannerinput){
try{
Stringtemp="";
while(input.hasNextInt()){
temp+=input.nextInt()+",";
}
String[]nums=temp.split(",");
int[]arr=newint[nums.length];
for(intindex=0;index<nums.length;index++){
arr[index]=Integer.parseInt(nums[index]);
}
returnarr;
}catch(Exceptione){
e.printStackTrace();
}
returnnull;
}
/**
*合并数组
*
*@parama
*数组1
*@paramb
*数组2
*@return整形数组
*/
publicint[]merge(int[]a,int[]b){
intlen=a.length;
if(b.length<len){
len=b.length;
}
int[]all=newint[a.length+b.length];
intindex=0;
intaIndex=0;
intbIndex=0;
while(aIndex<len||bIndex<len){
if(a[aIndex]<b[bIndex]){
all[index]=a[aIndex];
aIndex++;
}else{
all[index]=b[bIndex];
bIndex++;
}
index++;
}
if(aIndex<a.length){
while(aIndex<a.length){
all[index++]=a[aIndex++];
}
}else{
while(bIndex<b.length){
all[index++]=b[bIndex++];
}
}
returnall;
}
/**
*写入文件
*
*@paramprint
*PrintStream
*@parama
*数组
*/
publicvoidwriteFile(PrintStreamprint,int[]a){
for(intindex=0;null!=a&&index<a.length;index++){
print.append(a[index]+" ");
}
}
publicstaticvoidmain(String[]args){
MergeFilemerge=newMergeFile();
if(null!=args&&args.length>2){//输入参数合法,则使用,否则按照默认
merge.afile=args[0];
merge.bfile=args[1];
merge.mergefile=args[2];
}else{
System.out.println("Usingthedefaultfile");
}
Scannerinput=null;
int[]a=null;
int[]b=null;
int[]all=null;
try{
input=newScanner(newFile(merge.afile));
a=merge.readFile(input);
input=newScanner(newFile(merge.bfile));
b=merge.readFile(input);
all=merge.merge(a,b);
PrintStreamprint=newPrintStream(newFile(merge.mergefile));
merge.writeFile(print,all);
}catch(Exceptione){
e.printStackTrace();
}
}
}