导航:首页 > 文件教程 > 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文件相关的资料

热点内容
网络上说的陈劲生是什么 浏览:913
网站首页都用了哪些知识点 浏览:41
如何删除手机软件的重复文件 浏览:908
微信页面没有添加图标 浏览:47
暗黑绿色装备升级 浏览:261
到哪里学编程啊 浏览:752
电脑粉碎文件和卸载 浏览:365
怎么查看共享电脑所有文件 浏览:617
创意编程社区账号在哪里 浏览:377
好用的压缩文件 浏览:538
360下载的补丁包在哪个文件夹 浏览:988
微信54安卓版本官网 浏览:698
为什么cnc编程找工作难 浏览:777
sql数据库端口不通 浏览:361
javaword转swf 浏览:174
cms数据更新是什么 浏览:39
电脑保密柜在文件里怎么找不到了 浏览:225
nodejs前端后端 浏览:129
程序侠后台多少 浏览:32
mysqle执行sql文件在哪里 浏览:466

友情链接