-3

First text file A.txt;

asdfghjklqw12345 qwe3456789
asdfghjklqw12345 qwe3456789

Second text file B.txt;

|Record 1: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month| |Record 2: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month|

Third text file C.txt;

asdfghjklqw12345 qwe3456789 |Record 1: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month|

asdfghjklqw12345 qwe3456789 |Record 2: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month|

for the above situation where I want to merge two lines from two different text files into one line.My code is below

    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();
    }
Matt Clark
  • 27,671
  • 19
  • 68
  • 123
Avisek Panda
  • 19
  • 1
  • 1
  • 2

4 Answers4

1

Here is code:

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);
        }        
    }
}
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • I'm not the downvoter, but your while should have && instead of ||. Not to mention StringBuilder/StringBuffer instead of += on strings. So yeah, he had reasons. Enough reasons! – Silviu Burcea Sep 27 '13 at 12:15
  • I am getting this exception --->java.util.NoSuchElementException,at line java.util.NoSuchElementException – Avisek Panda Sep 28 '13 at 17:55
  • @AvisekPanda Provide some code and stack trace. You should update your question. – Silviu Burcea Oct 02 '13 at 15:17
0

You might want to have a look at BufferedReader and BufferedWriter. Show us what you tried and where you are stuck and we are happy to provide more help.

Kayz
  • 637
  • 1
  • 8
  • 21
0

Merging all txt file from a folder can be done in the following way:

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();    
    }
wahidd
  • 77
  • 1
  • 5
0

Improved on Masudul's answer to avoid compilation errors:

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());
        }
    }
}
Mathomatic
  • 899
  • 1
  • 13
  • 38