導航:首頁 > 文件教程 > java合並多個txt文件

java合並多個txt文件

發布時間:2024-04-21 20:18:09

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合並TXT文件數據

ok!我給你寫完了,首先你在D盤下面見兩個文件
分別是 file1.txt和file2.txt
如下所示
file1.txt
-----------------------------
id|name|class
1|test|class1
2|test2|class2
3|test3|class3

file2.txt
-----------------------------------
id|sex|teacher
1|male|wang
2|female|zhang
3|male|li

------------------------------------------
程序如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class CombinFile {
private final String FILE1 = "d:\\file1.txt";
private final String FILE2 = "d:\\file2.txt";
private final String FILENAME="d:\\combinFile.txt";

private InputStream getFileStream(String fileName) throws FileNotFoundException{
FileInputStream fis = new FileInputStream(new File(fileName));
return fis;
}

private void combinFile() throws IOException{
FileOutputStream fos = new FileOutputStream(new File(FILENAME));
InputStream fis1 = getFileStream(FILE1);
InputStream fis2 = getFileStream(FILE2);
int len = 0;
while((len=fis1.read())!=-1){
fos.write(len);
}
System.out.println("第一個文件已經寫到目標文件.....");
fis1.close();
fos.flush();

while((len=fis2.read())!=-1){
fos.write(len);
}

System.out.println("第二個文件已經寫到目標文件");
fis2.close();
fos.flush();
fos.close();

System.out.println("文件整合完畢");
}

public static void main(String[] args) {
try {
new CombinFile().combinFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}

--------------------------------------------------------
程序運行如下:
產生combinFile.txt文件,內容如下
id|name|class
1|test|class1
2|test2|class2
3|test3|class3
id|sex|teacher
1|male|wang
2|female|zhang
3|male|li

⑶ java合並兩個txt文件並生成新txt

importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.PrintStream;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.LinkedList;
importjava.util.Scanner;

publicclassTest
{
publicstaticfinalStringLINE=System.getProperty("line.separator");

publicstaticint[]readfile(Scannerinput)
{
int[]a=newint[0];
while(input.hasNextLine())
{
Stringline=input.nextLine().trim();
int[]dest=newint[a.length+1];
System.array(a,0,dest,0,a.length);
dest[dest.length-1]=Integer.parseInt(line);
a=dest;
}
input.close();
returna;
}

publicstaticvoidwritefile(PrintStreamoutput,int[]a)
{
output.append(Arrays.toString(a).replaceAll("[\[\]\s]","").replaceAll(",",LINE));
output.flush();
output.close();
}

publicstaticint[]merge(int[]a,int[]b)
{
int[]dest=newint[a.length+b.length];
System.array(a,0,dest,0,a.length);
System.array(b,0,dest,a.length,b.length);
returndest;
}
}

classTest1
{
publicstaticvoidmain(String[]args)
{
if(args.length!=3)
{
System.err.println("輸入的參數個數不是3個");
return;
}
try
{
Scannerinput=newScanner(newFile(args[0]));
int[]a=Test.readfile(input);
input=newScanner(newFile(args[1]));
int[]b=Test.readfile(input);
int[]dest=Test.merge(a,b);
try
{
PrintStreamoutput=newPrintStream(args[2]);
Test.writefile(output,dest);
}
catch(FileNotFoundExceptione)
{
e.printStackTrace();
}
}
catch(FileNotFoundExceptione)
{
e.printStackTrace();
}
}
}

classTest2
{
publicstaticvoidmain(String[]args)
{
if(args.length!=1)
{
System.err.println("輸入的參數個數不是1個");
return;
}
try
{
Scannerinput=newScanner(newFile(args[0]));
int[]dest=Test.readfile(input);
Arrays.sort(dest);
PrintStreamoutput=newPrintStream(args[0]);
Test.writefile(output,dest);
}
catch(FileNotFoundExceptione)
{
e.printStackTrace();
}
}
}

<Test3>
{
inti;

publicTest3(inti)
{
this.i=i;
}

@Override
publicintcompareTo(Test3o)
{
if(o.i>i)
{
return-1;
}
elseif(o.i<i)
{
return1;
}
else
{
return0;
}
}

@Override
publicStringtoString()
{
returnString.format("%s",i);
}

publicstaticvoidmain(String[]args)
{
if(args.length!=3)
{
System.err.println("輸入的參數個數不是3個");
return;
}
LinkedList<Test3>list=newLinkedList<Test3>();
try
{
Scannerinput1=newScanner(newFile(args[0]));
Scannerinput2=newScanner(newFile(args[1]));
while(true)
{
try
{
inta=Integer.parseInt(input1.nextLine().trim());
intb=Integer.parseInt(input2.nextLine().trim());
Test3ta=newTest3(a);
Test3tb=newTest3(b);
list.add(ta);
list.add(tb);
}
catch(Exceptione)
{
break;
}
}
input1.close();
input2.close();
Collections.sort(list);
PrintStreamoutput=newPrintStream(args[2]);
output.append(list.toString().replaceAll("[\[\]\s]","").replaceAll(",",Test.LINE));
output.flush();
output.close();
}
catch(FileNotFoundExceptione)
{
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盤的.txt2.txt3.txt文件合並成out.txt文件。
publicstaticvoidmain(String[]args){
mergeFiles("D:/output.txt",newString[]{"D:/1.txt","D:/2.txt","D:/3.txt"});
}
}

⑸ 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如何高效合並多個文件

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合並多個txt文件相關的資料

熱點內容
jspapijar官網下載 瀏覽:366
html調用文本文件 瀏覽:921
想學數控編程哪裡好 瀏覽:860
js獲取系統動態時間間隔 瀏覽:165
win10改win7進pe卡住 瀏覽:456
u盤中毒ink文件 瀏覽:718
蘋果換機數據遷移包含哪些數據 瀏覽:234
程式控制可編程直流電源在哪裡 瀏覽:598
容積長寬高的數據從什麼面測量 瀏覽:978
蘋果手游工作室 瀏覽:362
數據型號特別多怎麼合並 瀏覽:600
企業微信電腦版怎麼無法接受文件 瀏覽:968
微信個人相冊怎麼隱藏 瀏覽:368
上傳文件時怎麼獲取文件全路徑 瀏覽:955
linuxjs讀取文件 瀏覽:104
exe後綴的pdf文件 瀏覽:381
win8升級到專業版 瀏覽:487
快圖瀏覽quickpiciphone 瀏覽:847
app線稿圖需要哪些要求 瀏覽:752
java只能輸入漢字 瀏覽:630

友情鏈接