Ⅰ 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();
}
}
}