Java比较两个字符串仍然抛出true

Java比较两个字符串仍然抛出true,java,Java,Hy,我在比较两个空字符串时遇到了一个问题 public void run(){ System.out.println(properties.readConfig("port.cfg")); String lport = ""; String lasd = ""; File fport = null; File fasd = null; try { BufferedWriter writer = new BufferedWriter

Hy,我在比较两个空字符串时遇到了一个问题

public void run(){
    System.out.println(properties.readConfig("port.cfg"));
    String lport = "";
    String lasd = "";
    File fport = null;
    File fasd = null;
    try {
        BufferedWriter writer = new BufferedWriter( new FileWriter("port.cfg"));//this (if file doent exist) will create it
        BufferedWriter witer = new BufferedWriter( new FileWriter("asd.cfg"));//this (if file doent exist) will create it
        writer.close();
        witer.close();
        BufferedReader reader = new BufferedReader( new FileReader("port.cfg"));
        BufferedReader rader = new BufferedReader( new FileReader("asd.cfg"));
            lport = reader.readLine();
            lasd = rader.readLine();
        reader.close();
        rader.close();
        fasd = new File("asd.cfg");
        fport = new File("port.cfg");
        commands.print(String.valueOf(StringUtils.equals(lasd, lport)));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        temp = InetAddress.getLocalHost().toString().split("/");
        settings.myIp = temp[1];
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }           
    if(StringUtils.equals(lasd, lport)){//here is the problem
        properties.saveConfig("port.cfg", "8795");
        settings.port = properties.readConfig("port.cfg");
        commands.print("Current port is: " + settings.port);
    }else{
        settings.port = properties.readConfig("port.cfg");
    }



}  
当我第一次运行程序时,问题部分抛出true并写入文件
port.cfg
一个端口,用户可以更改此端口(这很好),但当我第二次运行程序时,它将再次抛出true并重写端口(这很糟糕,因为文件
port.cfg(String lport)中的文本)
与文件中的
asd.cfg(String lasd)
命令和
设置是我的类。
StrungUtils
类来自
apache.common.lang

编辑:

我试过:
lport==lasd,lport==null&&lasd==null

 StringUtils.equals(null, null) = true
对于这两个
null
StringUtils
将返回
true
。您应该在
StringUtils
之前添加
null
检查


我们不知道如何实现
StringUtils.equals()
。例如,此
抛出
运行时异常

public class Demo {

    public static void main(String[] args) {

        String a = null;
        String b = null;

        System.out.println(StringUtils.equals(a, b));
    }
}

class StringUtils {

    public static final boolean equals(String a, String b) {
        return a.equals(b);
    }
}
另一方面,
返回true

public static final boolean equals(String a, String b) {
    if(a == null && b == null) {
        return true;
    }
    else if(a == null || b == null) {
        return false;
    }
    else {
        return a.equals(b);
    }
}

如果不知道
StringUtils.equals()
的实现,我们将无法帮助您。或者编写您自己的
equals()
方法,就像我在这里以两种不同的方式所做的那样,或者学习程序中包含的API并相应地使用它们。

你的问题毫无意义-值
true
不能抛出-只有异常可以抛出。你真正的意思是什么?如果你的意思是“
StringUtils.equals(null,null)
返回true”那么你应该这么说…注意,我怀疑你的问题可能会在大约5行代码而不是40行代码中显示出来…我们也不知道你在谈论哪个
StringUtils
类也没有帮助…我发布了整个代码,因为我知道你们中的一些人可能想看到整个代码,因为你可能想知道m是lport和lasd,我记下了问题在哪里。我不明白你的问题。你是在问为什么这两个字符串都为null,或者为什么当两个参数都为null时if条件返回true?不,我们通常不希望看到大量不相关的代码。最好提供一个简短但完整的程序来演示问题,以及文本中的一些上下文。这段代码的大部分内容都是完全无关的。请注意,如果您发布了一个简短但完整的程序开始,那么很明显您是在使用Apache Commons。好的,当我使用时,我会问您一件事:File file1=new File(“port.cfg”);//contain 8950,File file2=new File(“asd.cfg”);//空文件,布尔值isTwoEqual=FileUtils.contentEquals(file1,file2);然后它仍然抛出true,为什么?我已经读到,如果其中一个为null,它将抛出false.ok,但当我从port.cfg(包含8975)到lport和从asd.cfg(空文件)读取第一行(这是文件中唯一的一行)时到lasd,并将其与StringUtils.equals进行比较,它仍然返回true为什么?当lport=8975和lasd=null时(当我打印到控制台lasd时,它返回null)在将值读入程序以确定值的实际内容后,是否打印出值?这两个值可能都是空格或null,或者天知道是什么…请执行以下操作:System.out.println(“[”+a+“][“+b+”]);并告诉我从控制台[null][null]打印出的内容我在这里看到了一些问题,因为即使我在文件中写入了一些内容,我仍然收到[null][null]好的,你的第一个目标是将值放入变量中。一旦这样做起作用,你就可以担心其他问题。我想我已经解决了,变量有问题,因为(我不知道为什么)当我把它们从尝试/捕捉循环中放出来时,它就起作用了:你能告诉我为什么吗?
public static final boolean equals(String a, String b) {
    if(a == null && b == null) {
        return true;
    }
    else if(a == null || b == null) {
        return false;
    }
    else {
        return a.equals(b);
    }
}