简体   繁体   English

我可以将 if-else 用于文件阅读器吗

[英]Can I use if-else for filereader

Can I use if-else statement for filereader?我可以对文件阅读器使用 if-else 语句吗? I tried to use '==' but it doesn't seem to work.我尝试使用 '==' 但它似乎不起作用。 I'm sorry if the question is a bit unintelligent, as I'm new to Java.如果问题有点笨拙,我很抱歉,因为我是 Java 新手。

Thank You!谢谢你!

String Path = new File("").getAbsolutePath();
readFile = new FileReader(Path + "/src/agents_chats_analytics.csv");
br = new BufferedReader(readFile);
...
if(readFile == new FileReader(Path+"/src/agents_chats_analytics.csv");))
 {
   // do stuffs
 }
else if(readFile == new FileReader(Path+"/src/department_agents.csv");)
 {
   // do stuffs
 }
...

So, It was really hard to understand what you are trying to achieve, but, I guess, I got you right, so you can do it this way:所以,真的很难理解你想要实现的目标,但是,我想,我猜对了,所以你可以这样做:

    String Path = new File("").getAbsolutePath();
    String readedFilePath = Path + "/src/agents_chats_analytics.csv";
    FileReader readFile = new FileReader(readedFilePath);

    if (readedFilePath.equals(Path + "/src/agents_chats_analytics.csv")) {
        readFile = new FileReader(Path + "/src/agents_chats_analytics.csv");
        // do stuffs
    } else if (readedFilePath.equals(Path + "/src/agents_chats_analytics.csv")) {
        readFile = new FileReader(Path + "/src/agents_chats_analytics.csv");
        // do stuffs
    }

You need to compare pathes not file readers.您需要比较路径而不是文件阅读器。 Also, logical equality of object types in java should be done using equals method and not " == " operator.此外,java 中对象类型的逻辑相等应该使用equals方法而不是“ == ”运算符来完成。 " == " will compare object references, so in your case such comparison always gives false . == ”将比较对象引用,因此在您的情况下,这种比较总是给出false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM