帮助,我获取了我电脑的ID,并将其保存到一个文本文件中,然后与包含ID的文本文件进行比较,但它说它们是不一样的。我不知道我做错了什么。
下面是代码。
public static void main(String[] args) throws Throwable {
Process process = Runtime.getRuntime().exec(new String[] { "wmic", "csproduct", "get", "UUID" });
process.getOutputStream().close();
Scanner sc = new Scanner(process.getInputStream());
String property = sc.next();
String serial = sc.next().toString();
try {
FileWriter myWriter = new FileWriter("Program/TextFiles/Serial.txt");
myWriter.write(serial);
myWriter.close();
} catch (IOException a)
{System.out.println(a);
}
File file = new File("Program/TextFiles/Serial.txt");
Scanner scan = new Scanner(file);
while (scan.hasNextLine())
System.out.println(scan.nextLine());
File file2 = new File("Program/TextFiles/Serial2.txt");
Scanner scan2 = new Scanner(file2);
while (scan2.hasNextLine())
System.out.println(scan2.nextLine());
if(scan == scan2){
System.out.println("yes");
}else {
System.out.println("no");
}
}
}
正如你的问题的评论中所提到的,你要检查的是 文件内容 是相等的,而不是如果 Scanner
用于读取文件的对象是相等的。
要检查文件内容,你有几个选项,请参见 这个问题 的想法。