① 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 怎麼合並兩個wav文件
//幫你寫了一個,是兩個mp3文件的合並
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
/**
*把兩個.mp3文件合並成一個.mp3文件
*
*@authorwangran
*
*/
publicclassMerger{
publicMerger(){
}
publicstaticvoidmain(String[]args){
FileInputStreamfis=null;
FileOutputStreamfos=null;
BufferedInputStreambis=null;
BufferedOutputStreambos=null;
//源文件
Filein1=newFile("D:/雜/娛樂/音樂/hero.mp3");
Filein2=newFile("D:/雜/娛樂/音樂/carelesswhisper.mp3");
//目標文件
Fileout=newFile("D:/music2.mp3");
//進行流操作
try{
fis=newFileInputStream(in1);
fos=newFileOutputStream(out,true);
bis=newBufferedInputStream(fis);
bos=newBufferedOutputStream(fos);
intlen;
byte[]buf=newbyte[1024];
while((len=bis.read(buf))!=-1){
bos.write(buf,0,len);
}
bos.flush();
fis=newFileInputStream(in2);
bis=newBufferedInputStream(fis);
while((len=bis.read(buf))!=-1){
bos.write(buf,0,len);
}
bos.flush();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
//關閉流
if(bis!=null)
try{
bis.close();
}catch(IOExceptione){
e.printStackTrace();
}
if(bos!=null)
try{
bos.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
③ 如何使用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 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如何高效合並多個文件
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中如何將兩個文件合並到另一個文件
java可以使用FileChannel快速高效地將多個文件合並到一起,以下是詳細代碼:
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){}
}
}
publicstaticvoidmain(String[]args){
mergeFiles("D:/output.txt",newString[]{"D:/in_1.txt","D:/in_2.txt","D:/in_3.txt"});
}
}