Java 我可以为filereader使用if-else吗
我可以为filereader使用if-else语句吗?我试着使用“==”,但似乎不起作用。 如果这个问题有点不明智,我很抱歉,因为我是Java新手 谢谢大家!Java 我可以为filereader使用if-else吗,java,eclipse,Java,Eclipse,我可以为filereader使用if-else语句吗?我试着使用“==”,但似乎不起作用。 如果这个问题有点不明智,我很抱歉,因为我是Java新手 谢谢大家! String Path = new File("").getAbsolutePath(); readFile = new FileReader(Path + "/src/agents_chats_analytics.csv"); br = new BufferedReader(readFile); ... if(readFile == n
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
}
...
所以,很难理解你想要实现什么,但是,我想,我是对的,所以你可以这样做:
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
}
您需要比较路径而不是文件读取器。
此外,java中对象类型的逻辑相等应该使用
equals
方法而不是“=
”操作符来完成。“==
”将比较对象引用,因此在您的情况下,这种比较总是给出false
您到底想做什么?我正在尝试读取3个excel文件。如果文件与readFile匹配,它将运行代码,如果不匹配,它将转到下一个,如果我认为您可能需要文件
。使用File newFile=newFile(路径“/src/etc.”)
将两个文件路径连接在一起。然后使用newFile.getName()
获取包含路径中最后一个file元素的字符串。“如果文件与readFile匹配”是什么意思?什么文件?匹配readFile意味着什么?@Oleg刚才-if(readFile==newfilereader(Path+“/src/agents\u chats\u analytics.csv”))
匹配readFile=newfilereader(Path+”/src/agents\u chats\u analytics.csv”)
然后if将运行代码