Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java/Javascript:文件逐行比较,忽略某些部分_Java_File_Comparison - Fatal编程技术网

Java/Javascript:文件逐行比较,忽略某些部分

Java/Javascript:文件逐行比较,忽略某些部分,java,file,comparison,Java,File,Comparison,问题: 有没有更好的方法来比较两个小文件(100Kb),同时选择性地忽略文本的某一部分。并报告差异 查找默认/现有java库或任何windows本机应用程序 以下是场景: Expected file 1 located at D:\expected\FileA_61613.txt ..Actual file 2 located at D:\actuals\FileA_61613.txt 但是,当同一行中存在多个差异时。我无法将它们全部记录下来,因为我是逐字符比较,而不是逐字比较。 我不喜欢逐

问题: 有没有更好的方法来比较两个小文件(100Kb),同时选择性地忽略文本的某一部分。并报告差异

查找默认/现有java库或任何windows本机应用程序

以下是场景:

Expected file 1 located at D:\expected\FileA_61613.txt ..Actual file 2 located at D:\actuals\FileA_61613.txt 但是,当同一行中存在多个差异时。我无法将它们全部记录下来,因为我是逐字符比较,而不是逐字比较。

我不喜欢逐字比较,因为我认为不可能比较换行符和空格。我的理解正确吗?

java.lang.StringIndexOutOfBoundsException
来自以下代码:

for (int i = 0; i < strLine1.length(); i++) {
   if (strLine1.charAt(i) != strLine2.charAt(i)) {
       System.out.println("char not same at " + i);
   }   
}
for(int i=0;i

当您将更大的
String
strLine滚动到一个索引时,该索引大于strLine2的长度(第二个文件比第一个文件小),您将得到该异常。它之所以出现,是因为strLine2较短时,这些索引上没有值。

到目前为止您做了什么?最好展示您尝试过的东西,而不是让我们为您的freeJava或JavaScript工作?这是两种完全不同的语言。我用代码更新了线程。我试图知道是否有方法/默认api功能或任何具体的解决方案可以使用,而不是我使用的所有if、while和for循环。我能够找出文件中的差异,然后下一个任务是忽略所选文本。当我尝试实现它时,它跳过了下一行。这个主题被称为[暂停],是否有我需要更新的内容或任何其他细节。我正在用Java尝试这个。即使javascript被认为很容易实现,我也可以尝试。是的,我更新了基于maxIndex的代码执行比较。 Some first line here There may be whitespaces, line breaks, indentation and here is another line Key : SomeValue Date : 18/09/2013 Timestamp : 15:10.345+10.00 key2 : Value2 key3 : Value3 key4 : Something Different key5 : Value5 Some other text again to indicate that his is end of this file.
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class fileComparison {
    public static void main(String[] args) throws IOException {
        FileInputStream fstream1 = new FileInputStream(
                "D:\\expected\\FileA_61613.txt");
        FileInputStream fstream2 = new FileInputStream(
                "D:\\actuals\\FileA_61613.txt");
        DataInputStream in1 = new DataInputStream(fstream1);
        BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
        DataInputStream in2 = new DataInputStream(fstream2);
        BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
        int lineNumber = 0;
        String strLine1 = null;
        String strLine2 = null;
        StringBuilder sb = new StringBuilder();
        System.out.println(sb);
        boolean isIgnored = false;

        while (((strLine1 = br1.readLine()) != null)
                && ((strLine2 = br2.readLine()) != null)) {
            lineNumber++;
            if (!strLine1.equals(strLine2)) {
                int strLine1Length = strLine1.length();
                int strLine2Length = strLine2.length();
                int maxIndex = Math.min(strLine1Length, strLine2Length);
                if (maxIndex == 0) {
                    sb.append("Mismatch at line " + lineNumber
                            + " all characters " + '\n');
                    break;
                }
                int i;
                for (i = 0; i < maxIndex; i++) {
                    if (strLine1.charAt(i) == '#') {
                        isIgnored = true;
                        continue;
                    }
                    if (strLine1.charAt(i) != strLine2.charAt(i)) {
                        isIgnored = false;
                        break;
                    }
                }
                if (isIgnored) {
                    sb.append("Ignored line " + lineNumber + '\n');
                } else {
                    sb.append("Mismatch at line " + lineNumber + " at char "
                            + i + '\n');
                }
            }
        }
        System.out.println(sb.toString());
        br1.close();
        br2.close();

    }
}
Ignored line 7
Mismatch at line 8 at char 4
Mismatch at line 11 at char 13
Mismatch at line 12 at char 8
Mismatch at line 14 all characters 
for (int i = 0; i < strLine1.length(); i++) {
   if (strLine1.charAt(i) != strLine2.charAt(i)) {
       System.out.println("char not same at " + i);
   }   
}