gokul
gokul

Reputation: 21

comparing 2 files using java

i have 2 text files, file_1: and the other file2

file_1:

tango 12
beta 256
alpha 1700
tango 1200

file_2:

tango 12
tango 125 hello world
beta 256
alpha 1700

i need to remove lines in file_2 which contains file_1 entries. ie. read line by line from file_2, compare with file_1 lines, if match , remove , else append to a new file. matches are exact string matches, both are text files. can any one tell me how can i do it using java

Upvotes: 2

Views: 2773

Answers (2)

Jigar Joshi
Jigar Joshi

Reputation: 240870

Read both file in to Collection of String.

Then compare two List and process accordingly.

Rest all things are given by duffymo , Here is how to compare two arraylist of String

Upvotes: 1

duffymo
duffymo

Reputation: 308743

Break the problem into steps:

  1. Read a file into a list of strings
  2. Iterate over two collections of strings and compare them
  3. Write to a file

Upvotes: 4

Related Questions