This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Compare text files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compare text files" Watch "Compare text files" New topic
Author

Compare text files

Monish Chandrasekar
Greenhorn

Joined: Feb 07, 2013
Posts: 2
Hi,

Im trying to compare two text files in java, I wanted the source file and target file to be displayed side by side and the line which is different needs to be highlighted

I tried with this code, But it only shows the difference, what will I need to change to display two files side by side Like in http://www.comparemyfiles.com/



Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2169
    
  47
If you want to do it like the link you'll need a GUI.
If you want to keep it as CLI, print out the files side by side with some marker to show lines that don't match then add a print statement to the if statement that compares the lines for equality. You will need to change the chars you insert between the two concatenated lines so you can see which lines are not the same.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
And welcome to the Ranch
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41079
    
  43
If you want to minimize your work, use Runtime.exec to run "diff" on the two files and do something with its output (You'd need Unix/Linux underneath, or UnxUtils installed on Windows, though.)

Or check out http://jlibdiff.sourceforge.net/ and http://code.google.com/p/google-diff-match-patch/


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
Don’t use StringTokenizer (← click here and it tells you why).
Don’t use a 10000 element array. The alternatives to tokenizer can produce an array of the right size.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compare text files
 
Similar Threads
Java I/O for two file
Problem in file transfer
File Comaprison
Adding Ascii / Binary tokens from .txt files
Uploading jpeg images to webserver