小编an3*_*nto的帖子

使用.equals()比较两个字符串返回False,但它们的字节数组相等

尝试将图像从客户端发送到服务器时,我遇到了一些麻烦,因为原始图像与收到的图像不同.为了找到问题,我正在逐行阅读两个图像寻找差异.当我逐行比较字符串时,对于某些使用String#equals的行(例如lineo.equals(lined)),结果为false,但是当我在控制台中打印它们时它们似乎相同,我也比较它们的字节阵列.令人惊讶的是,使用Array.equals(lineo.getBytes(),lined.getBytes())结果为true.客户端和服务器都在同一台计算机上.

请帮我理解

  1. 我在哪里可以找到两个字符串之间的区别
  2. 为什么两种方法都要比较,返回不同的结果

    private void compareImages() throws IOException {
        File dest = new File("C:\\TempFiles\\" + fileName);
        File orig = new File("C:\\Users\\Andres\\Desktop\\B&N\\" + fileName);
    
        BufferedReader bro = new BufferedReader(new FileReader(orig));
        BufferedReader brd = new BufferedReader(new FileReader(dest));
    
        String lineo = bro.readLine();
        String lined = brd.readLine();
        System.out.println("Ready to read");
        while (lineo!= null && lined!= null) {
            if(!lined.equals(lineo))
            {
                System.out.println("lineo");
                System.out.println(lineo);
                System.out.println("lined");
                System.out.println(lined);
                System.out.println("arrayo");
                System.out.println(printArray(lineo.getBytes()));
                System.out.println("arrayd");
                System.out.println(printArray(lined.getBytes()));
                System.out.println("Are: " + Arrays.equals(lined.getBytes(),     lineo.getBytes()));
                break;
            }
            lineo = bro.readLine();
            lined = brd.readLine();
        }
        bro.close();
        brd.close(); …
    Run Code Online (Sandbox Code Playgroud)

java string byte equals

6
推荐指数
1
解决办法
2882
查看次数

标签 统计

byte ×1

equals ×1

java ×1

string ×1