Wednesday, 15 July 2015

java - How to merge data from two text file -



java - How to merge data from two text file -

i have 2 related text files shown illustration in data1.txt , data2.txt. want merge 2 files create result.txt. thought how go this?

data1.txt

books, 3 shelf, 5 groceries,6 books, 1 shelf, 2

data2.txt

books,2 shelf,3 groceries,1

result.txt

books, 3, 2 shelf, 5,3 groceries,6,1 books, 1,2 shelf, 2, 3

this illustration you.first need add together values 2d list data2 text file.and when line null in file2 can mapping value relative it's text list .so have method homecoming mapping value string .code little long thought .i post relevant methods here.this link finish class file

public void marged(){ seek { bufferedreader br1 = null; bufferedreader br2 = null; string line1; string line2; arraylist<arraylist<string>> arraylist = new arraylist<>(); br1 = new bufferedreader(new filereader("c:\\users\\madhawa.se\\desktop\\workingfox\\data1.txt")); br2 = new bufferedreader(new filereader("c:\\users\\madhawa.se\\desktop\\workingfox\\data2.txt")); while ((line1 = br1.readline()) != null) { string[] split1 = line1.split(","); string line1word = split1[0].trim(); string line1val = split1[1].trim(); line2 = br2.readline(); if (line2 != null) { string[] split2 = line2.trim().split(","); string line2word = split2[0].trim(); string line2val = split2[1].trim(); arraylist<string> list = new arraylist(); list.add(line2word); list.add(line2val); arraylist.add(list); if (line1word.equalsignorecase(line2word)) { string ok = line1word + "," + line1val + "," + line2val; system.out.println(ok); } } else { string ok = line1word + "," + line1val + "," + doesexist(arraylist, line1word); system.out.println(ok); } } } grab (exception ex) { ex.printstacktrace(); } }

this method homecoming mapping value

public string doesexist(arraylist<arraylist<string>> arraylist, string s) { (int = 0; < arraylist.size(); i++) { string = arraylist.get(i).get(0); if (get.trim().equalsignorecase(s.trim())) { homecoming arraylist.get(i).get(1); } } homecoming "-1"; }

output>>

books,3,2 shelf,5,3 groceries,6,1 books,1,2 shelf,2,3

java text merge

No comments:

Post a Comment