sdsssfa2222 2021-12-25 21:14 采纳率: 100%
浏览 27
已结题

java自学,怎么对这两个文本内容比较,如果一样就输出打印成功。到最后找不到方法名,不知道怎么比较



import java.io.FileInputStream;

public class Tiqu {
    public static void main(String[] args) {
        try {
            FileInputStream in = new FileInputStream("D:\\eclipse\\lianxi\\day19\\src\\day10\\haha1.txt");
            byte[] b = new byte[100];
            int len = 0;
            while ((len = in.read(b)) != -1) {
                System.out.println(new String(b, 0, len));
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            FileInputStream in1 = new FileInputStream("D:\\JAVA\\haha2.txt");
            byte[] b = new byte[100];
            int len = 0;
            while ((len = in1.read(b)) != -1) {
                System.out.println(new String(b, 0, len));
            }
            in1.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
if(in()=in1()){
    System.out.println("打印成功");
    }
}
  • 写回答

2条回答 默认 最新

  • XugeS 2021-12-25 22:46
    关注
    
    package Test_;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    
    /**
     * @author yjx
     * @version 1.0
     */
    public class Test2 {
        public static void main(String[] args) {
            StringBuilder str1= null;
            StringBuilder str2= null;
            FileInputStream in=null;
            try {
                in = new FileInputStream("D:\\JAVA\\haha2.txt");
                byte[] b = new byte[100];
                int len = 0;
    
                while ((len = in.read(b)) != -1) {
                    assert false;
                    str1.append(new String(b, 0, len));
                }
                in = new FileInputStream("D:\\eclipse\\lianxi\\day19\\src\\day10\\haha1.txt");
                while ((len = in.read(b)) != -1) {
                    assert false;
                    str2.append(new String(b, 0, len));
                }
               
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if(in!=null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            if(str1.equals(str2)){
                System.out.println("打印成功");
            }
    //      
    
        }
    
    
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月3日
  • 已采纳回答 12月26日
  • 创建了问题 12月25日

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码