Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

programatic diff tool

 
Joe Von
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a tool which (among other things) needs to determine if two text files are different and, if different, which line has changed.
Does anyone know of a tool or an API which I can code to which has this sort of functionality?
Thanks,
Joe
 
BJ Grau
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use the io package. Do something like this to read your files:
File f = new File("c:\\file.txt");
FileReader fr = new FileReader(f);
LineNumberReader lnr = new LineNumberReader(fr);
String lineText = "";
int lineNumber = 0;
while(text != null)
{
line = lnr.getLineNumber();
text = lnr.readLine();
}
This bit of code should read a file and grab each line, putting it into a String. Just do this to the two files you are interested in, and do a .equals between each line you read in.
Hope this helps.
 
BJ Grau
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! An error in my code:
line = lnr.getLineNumber();
text = lnr.readLine();

should read
lineNumber = lnr.getLineNumber();
lineText = lnr.readLine();
 
Joe Von
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool.
Thanks BJ.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic