導航:首頁 > 編程語言 > javatxt合並

javatxt合並

發布時間:2022-11-14 03:40:51

『壹』 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文件並生成新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();
}

}

}

『叄』 JAVA 合並文本文件內容

BufferedWriter 有緩沖區,如果寫入的數據沒超過緩沖區大小 是不會真正寫入文件的 當然也可以手動清空,用output.flush(); BufferedWriter 每次寫操作完都要記得調用這個方法的

『肆』 如何使用java合並多個文件

如何使用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文件

可以試下用BufferedReader和BufferedWriter進行文件讀寫,合並在讀的時候做就行了。

『陸』 用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文件合並,並且統一字元編碼

使用 java.io.FileInputStream和 java.io.InputStreamReader 可以根據編碼讀文件

寫文件則可以使用java.io.FileOutputStream 和 java.io.OutputStreamWriter

『捌』 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合並多個文件

使用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文件(每個文件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就可以,不用寫代碼

閱讀全文

與javatxt合並相關的資料

熱點內容
銀河v10驅動重編譯 瀏覽:889
電腦上文件夾右擊就會崩潰 瀏覽:689
右美維持演算法 瀏覽:938
php基礎編程教程pdf 瀏覽:219
穿越之命令與征服將軍 瀏覽:351
android廣播重復 瀏覽:832
像阿里雲一樣的伺服器 瀏覽:318
水冷空調有壓縮機嗎 瀏覽:478
訪問日本伺服器可以做什麼 瀏覽:432
bytejava詳解 瀏覽:448
androidjava7 瀏覽:384
伺服器在山洞裡為什麼還有油 瀏覽:885
天天基金app在哪裡下載 瀏覽:974
伺服器軟路由怎麼做 瀏覽:292
冰箱壓縮機出口 瀏覽:228
OPT最佳頁面置換演算法 瀏覽:644
網盤忘記解壓碼怎麼辦 瀏覽:853
文件加密看不到裡面的內容 瀏覽:654
程序員腦子里都想什麼 瀏覽:434
oppp手機信任app在哪裡設置 瀏覽:189