導航:首頁 > 編程語言 > java合並多個文件

java合並多個文件

發布時間:2024-03-17 12:16:23

java 怎麼樣把多個Excel 合並為一個

程序中引用spire.xls.jar包

  1. import com.spire.xls.*;


  2. public class MergeExcels {

  3. public static void main(String[] args){

  4. //將待合並Excel文檔的名稱保存至字元串數組

  5. String[] inputFiles = new String[]{"file1.xlsx","file2.xlsx"};


  6. //創建一個新的Excel文檔

  7. Workbook newBook = new Workbook();

  8. //清除默認的3張工作表

  9. newBook.getWorksheets().clear();


  10. //創建另一個Excel文檔

  11. Workbook tempBook = new Workbook();


  12. //遍歷數組,依次載入每個Excel文檔並將文檔中的所有工作表復制到新建的Excel文檔中

  13. for (String file : inputFiles)

  14. {

  15. tempBook.loadFromFile(file);

  16. for (Worksheet sheet : (Iterable)tempBook.getWorksheets())

  17. {

  18. newBook.getWorksheets().addCopy(sheet, WorksheetCopyType.CopyAll);

  19. }

  20. }


  21. //保存

  22. newBook.saveToFile("MergeFiles.xlsx", ExcelVersion.Version2013);

  23. }

  24. }

② 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"});
}
}
閱讀全文

與java合並多個文件相關的資料

熱點內容
python直接退出程序 瀏覽:842
百戰程序員收費標准 瀏覽:776
時鍾置換演算法指針變化規則 瀏覽:245
微信加密能否改密碼 瀏覽:104
android許可權組 瀏覽:169
2017單片機 瀏覽:476
讓孩子感興趣編程的電影 瀏覽:261
用顏料製作脆皮解壓球 瀏覽:932
火箭解壓器 瀏覽:72
cnet中級程序員面試題 瀏覽:190
單片機怎麼做人臉識別 瀏覽:154
監獄辦理工商銀行app怎麼辦呢 瀏覽:819
c語言寫編程時需要用什麼輸入法 瀏覽:590
生發程序員 瀏覽:167
高考英語pdf 瀏覽:418
哈利波特忘記伺服器怎麼辦 瀏覽:824
怎麼看其他電腦共享文件夾 瀏覽:513
py文件夾後綴 瀏覽:723
你對我們的app有什麼建議 瀏覽:584
phpgetcookie 瀏覽:145