简体   繁体   中英

Can I use if-else for filereader

Can I use if-else statement for filereader? 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.

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. " == " will compare object references, so in your case such comparison always gives false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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