-2

I have to text files and I want to print the line numbers where the contents of the second file is modified?

file 1:
1/ hi
2/ 
3/ 
4/ start
5/ {
6/ 
7/ while(){
8/ }
9/ }

file 2:
1/ hi
2/ 
3/ 
4/ 
5/ start
6/
7/
8/ {
9/ if(
10/ while(){
11/ }
12/ }.

The output should be: 9 (as an extra if is added in the file 2). Note, there might be unnecessary tabs and newlines.Can anyone help me with this problem?

import java.util.Scanner;



    //convert a file into a file_simple form
    File simplified(String src , String srcPath , String name) throws FileNotFoundException{
        File fileSrc = new File(src);
        File fileDest = new File(srcPath + "\\" + name + "_simple");
        Scanner scSrc = new Scanner(fileSrc);
        PrintWriter pw = new PrintWriter(fileDest);
        while(scSrc.hasNextLine()) {
            String take = scSrc.nextLine();
            if(take.equals("")) {
                continue;
            }
            String take1 = take.trim();
            pw.println(take1);
        }
        pw.close();
        scSrc.close();
        return fileDest;
    }

     //write what difference the second file has
    void differenceInTwoFiles(File file1 , File file2 , String logPath , String UserName) throws FileNotFoundException {
        File file1Simple = simplified(file1.getAbsolutePath() , file1.getParentFile().getAbsolutePath() , file1.getName());
        File file2Simple = simplified(file2.getAbsolutePath() , file2.getParentFile().getAbsolutePath() , file2.getName());
        System.out.println(file2.getAbsolutePath() +" " + file2.getParentFile().getAbsolutePath() +" "+ file2.getName());
        File log = new File(logPath); 
        PrintWriter pwLog = new PrintWriter(log);

        Scanner scF1 = new Scanner(file1Simple);
        Scanner scF2 = new Scanner(file2Simple);

        while(scF1.hasNextLine() && scF2.hasNextLine()) {
            String first = scF1.nextLine();
            String second = scF2.nextLine();
            if(!first.equals(second)) {
                pwLog.println(UserName + " has MODIFIED in " + file2.getName() + " : " + second);
            }
        }
        pwLog.close();
        scF1.close();
        scF2.close();
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

I vote for not using Java to handle this task, because there are already very good tools out there for doing along the lines of what you want. For example, Linux has a diff tool which might work well here:

diff file1.txt file2.txt

At the very least, the output from diff would flag every line which were in disagreement between the two files, and maybe just that would be enough to meet your requirements.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • but can i solve it in java? this is partial problem of my java project so.. – Ayon Roy Shouhag Oct 06 '18 at 16:29
  • You should have mentioned that this was homework, or at least that you needed to use Java. In this case, you can just iterate both files and then flag lines which don't agree. Any tutorial can get you started on how to do this. – Tim Biegeleisen Oct 06 '18 at 16:30
  • @AyonRoyShouhag [Just use a system call to the linux os using java](https://stackoverflow.com/questions/792024/how-to-execute-system-commands-linux-bsd-using-java) I think this would work – Albin Paul Oct 06 '18 at 16:31