使用java逐行合并两个文本文件
第一个文本文件使用java逐行合并两个文本文件,java,merge,dataoutputstream,Java,Merge,Dataoutputstream,第一个文本文件 A.txt asdfghjklqw12345 qwe3456789 asdfghjklqw12345 qwe3456789 第二个文本文件 B.txt |记录1:已拒绝-表授权TBL列AUTH_DATE.ORA-01843上的错误:不是有效月份| |记录2:已拒绝-表授权TBL列AUTH_DATE.ORA-01843上的错误:不是有效月份| 第三个文本文件 C.txt asdfghjklqw12345 qwe3456789 |记录1:拒绝-表格授权错误| TBL,列AUTH_DA
A.txt
asdfghjklqw12345 qwe3456789asdfghjklqw12345 qwe3456789 第二个文本文件
B.txt
|记录1:已拒绝-表授权TBL列AUTH_DATE.ORA-01843上的错误:不是有效月份|
|记录2:已拒绝-表授权TBL列AUTH_DATE.ORA-01843上的错误:不是有效月份|
第三个文本文件
C.txt
asdfghjklqw12345 qwe3456789 |记录1:拒绝-表格授权错误| TBL,列AUTH_DATE.ORA-01843:不是有效月份|
asdfghjklqw12345 qwe3456789 |记录2:拒绝-表授权错误| TBL,列AUTH_DATE.ORA-01843:不是有效月份|
对于上述情况,我想将两个不同文本文件中的两行合并为一行
List<FileInputStream> inputs = new ArrayList<FileInputStream>();
File file1 = new File("C:/Users/dell/Desktop/Test/input1.txt");
File file2 = new File("C:/Users/dell/Desktop/Test/Test.txt");
FileInputStream fis1;
FileInputStream fis2;
try {
fis1 = new FileInputStream(file1);
fis2= new FileInputStream(file2);
inputs.add(fis1);
inputs.add(fis2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int total = (int) (file1.length() + file2.length());
System.out.println("total length is " + total);
SequenceInputStream sis = new SequenceInputStream(Collections.enumeration(inputs));
try {
System.out.println("SequenceInputStream.available() = "+ sis.available());
byte[] merge = new byte[total];
int soFar = 0;
do {
soFar += sis.read(merge,total - soFar, soFar);
} while (soFar != total);
DataOutputStream dos = new DataOutputStream(new FileOutputStream("C:/Users/dell/Desktop/Test/C.txt"));
soFar = 0;
dos.write(merge, 0, merge.length);
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List inputs=new ArrayList();
File file1=新文件(“C:/Users/dell/Desktop/Test/input1.txt”);
File file2=新文件(“C:/Users/dell/Desktop/Test/Test.txt”);
文件输入流fis1;
文件输入流fis2;
试一试{
fis1=新文件输入流(文件1);
fis2=新文件输入流(文件2);
输入。添加(fis1);
输入。添加(fis2);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
inttotal=(int)(file1.length()+file2.length());
System.out.println(“总长度为”+总长度);
SequenceInputStream sis=新的SequenceInputStream(Collections.enumeration(inputs));
试一试{
System.out.println(“SequenceInputStream.available()=”+sis.available());
字节[]合并=新字节[总计];
int-soFar=0;
做{
soFar+=sis.read(合并,总计-soFar,soFar);
}while(soFar!=总计);
DataOutputStream dos=新的DataOutputStream(新文件OutputStream(“C:/Users/dell/Desktop/Test/C.txt”);
soFar=0;
写入(merge,0,merge.length);
dos.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
您可能需要查看和。
告诉我们您尝试了什么以及遇到了什么困难,我们很乐意提供更多帮助。以下是代码:
public class MergeText {
public static void main(String[] args) throws IOException{
String output="";
try(Scanner sc1=new Scanner((new File("A.txt")));
Scanner sc2=new Scanner((new File("B.txt")))){
while(sc1.hasNext() || sc2.hasNext()){
output+=sc1.next() +" "+ sc2.next();
output+="\n";
}
}
try(PrintWriter pw=new PrintWriter(new File("C.txt"))){
pw.write(output);
}
}
}
可以通过以下方式合并文件夹中的所有txt文件:
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<String>();
//Reading data files
try {
File folder = new File("path/inputFolder");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
File file = listOfFiles[i];
if (file.isFile() && file.getName().endsWith(".txt")) {
BufferedReader t = new BufferedReader (new FileReader (file));
String s = null;
while ((s = t.readLine()) != null) {
list.add(s);
}
t.close();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
//Writing merged data file
BufferedWriter writer=null;
writer = new BufferedWriter(new FileWriter("data.output/merged-output.txt"));
String listWord;
for (int i = 0; i< list.size(); i++)
{
listWord = list.get(i);
writer.write(listWord);
writer.write("\n");
}
System.out.println("complited");
writer.flush();
writer.close();
}
publicstaticvoidmain(字符串[]args)引发IOException{
ArrayList=新建ArrayList();
//读取数据文件
试一试{
文件夹=新文件(“路径/输入文件夹”);
File[]listOfFiles=folder.listFiles();
for(int i=0;i
改进了Masudul的答案以避免编译错误:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class MergeText {
public static void main(String[] args) throws IOException {
StringBuilder output = new StringBuilder();
try (Scanner sc1 = new Scanner((new File("C:\\Users\\YourName\\Desktop\\A.txt")));
Scanner sc2 = new Scanner((new File("C:\\Users\\YourName\\Desktop\\B.txt")))) {
while (sc1.hasNext() || sc2.hasNext()) {
String s1 = (sc1.hasNext() ? sc1.next() : "");
String s2 = (sc2.hasNext() ? sc2.next() : "");
output.append(s1).append(" ").append(s2);
output.append("\n");
}
}
try (PrintWriter pw = new PrintWriter(new File("C:\\Users\\mathe\\Desktop\\Fielddata\\RESULT.txt"))) {
pw.write(output.toString());
}
}
}
你想做什么?到目前为止你试过什么吗?如果是,请告诉我们!你试图达到什么目的?1 2 3 4 5是独立的行吗?您的说明与示例不符。另外,请分享你的代码。嗨,托马斯,请帮我找到上述问题的解决方案……我不是反对者,但你的while应该是&&而不是| |。更不用说字符串上的StringBuilder/StringBuffer而不是+=了。所以是的,他有理由。足够的理由!我在java.util行得到了这个异常-->java.util.NoSuchElementException。NoSuchElementException@AvisekPanda提供一些代码和堆栈跟踪。你应该更新你的问题。