Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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 逐字符比较两个文件_Java_Loops_While Loop - Fatal编程技术网

Java 逐字符比较两个文件

Java 逐字符比较两个文件,java,loops,while-loop,Java,Loops,While Loop,这是我的密码: public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Type first file name to use: "); String filename1 = console.nextLine(); System.out.print("Type second file name to use: ");

这是我的密码:

public static void main(String[] args) {
    Scanner console = new Scanner(System.in); 
    System.out.print("Type first file name to use: ");
    String filename1 = console.nextLine();
    System.out.print("Type second file name to use: ");
    String filename2 = console.nextLine();
    Scanner input1 = new Scanner(new File(filename1));
    Scanner input2 = new Scanner(new File(filename2));
    String s1 = input1.next();
    String s2 = input2.next();
    boolean similar=true;r
while(!input1.hasNext()&&!input2.hasNext()){
// this is where i am stuck. i need to compare the lines character by character, but i'm not sure how to do so. i can't use buffer method, or any really advanced methods since i am just a beginner, so sticking to the basic loops (while, iff, for.. ) is the only option i have
} 
该程序应提示用户输入两个文件名,并根据文件内容是否相同打印true或false。它应逐个字符检查文件,如果字符为字母,则忽略差异(大写和小写)。谢谢。

使用:

if (s1.equalsIgnoreCase(s2)) {
    // All characters on the entire line are equal.
} else {
    // There is a difference
}
它将一行一个字符地比较字符串


您可以创建一个循环,但这将需要更多的代码,并会导致性能下降。

一个字符接一个字符,并且不允许当前结构执行任何工作,那么

while(!input1.hasNext()&&!input2.hasNext())
{
  char c1 = input1.next(".");
  char c2 = input2.next(".");

  if (c1!=c2)
  {
    //Files are not equal
  }
}

如果愿意,您可以使用
Reader
实例逐字符处理文件。 比如:

BufferedReader reader1 = new BufferedReader(new FileReader("fileName1"));
BufferedReader reader2 = new BufferedReader(new FileReader("fileName2"));
int char1; // from first file
int char2; // from second file
// loop until the end of either file is reached
while ((char1 = reader1.read()) != -1 && (char2 = reader2.read()) != -1) {
    // convert current characters from both files to lowerCase
    char lowerCase1 = Character.toLowerCase((char) char1);
    char lowerCase2 = Character.toLowerCase((char) char2);
    if (lowerCase1 != lowerCase2) {
        System.out.println("your text files are different!");
        return;
    }
} 

你应该这样做:

public static void main(String[] args) {
    Scanner console = new Scanner(System.in); 
    System.out.print("Type first file name to use: ");
    String filename1 = console.nextLine();
    System.out.print("Type second file name to use: ");
    String filename2 = console.nextLine();
    Scanner input1 = new Scanner(new File(filename1));
    Scanner input2 = new Scanner(new File(filename2));
    String s1 = input1.next();
    String s2 = input2.next();
    boolean similar=true;r
    while(!input1.hasNext()&&!input2.hasNext()){
        if (a.findInLine(".").compareToIgnoreCase(b.findInLine(".")) != 0) {
            similar = false;
            break;
        }
    }

    if (similar && (a.hasNext() || b.hasNext())) // unequal sizes
        similar = false;
    System.out.println("Files are " + (similar ? "equal" : "unequal"));
    return 0;
}

我想知道您是否可以对文件进行散列并比较散列以节省大量时间或开销。假设您在评论的行中只有问题,您的问题实际上是“如何比较两个字符串”。在这种情况下,如果(!input1.next().equals(input2.next()){/*不等于*/},则使用equals:
。如果您真的需要逐字符,请对它们的字符使用循环,通过例如,
input1.next().toCharArray()
@user2860598:哈希需要检查整个文件,因此在这种情况下效率较低。也许您应该看看这个@user2860598:如果您逐个字符进行比较,一旦发现不匹配,就可以退出。在计算散列值时,您必须检查整个文件。使用next(“aRegex”)而不是在charAt(i)上循环有点过分——尽管Java可能正在对正则表达式进行一些很好的缓存,以避免所有对象的创建和删除。为了提高效率,我同意您的看法。虽然OP看起来像个初学者,但正则表达式解决方案会让人困惑。我试图找到一个简单且易于理解的解决方案,OP一旦理解了这个想法就可以修改它。但是next(“.”)与next(Pattern.compile(“.”)是等价的——事实上,你是在提倡使用regexesMeh,你是对的。我显然没有直截了当地思考。我的解决方案也是区分大小写的。我不允许使用Readeri,只是把它添加到我的while循环中?或者我应该创建一个返回true或false的布尔方法吗?@TRANDER44被编辑成几乎可以工作的解决方案。抱歉,未经测试,但应该有效。