Java文件的行为很奇怪
我真的对java奇怪的行为感到困惑 我的代码是Java文件的行为很奇怪,java,filenotfoundexception,Java,Filenotfoundexception,我真的对java奇怪的行为感到困惑 我的代码是 public class Main { private static String KK_PATH = "C:\\Users\\ahopyar\\Desktop\\kk.txt"; private static String EXCELL_PATH = "C:\\Users\\ahopyar\\Desktop\\kk.txt"; static List<User> users = new ArrayList&l
public class Main {
private static String KK_PATH = "C:\\Users\\ahopyar\\Desktop\\kk.txt";
private static String EXCELL_PATH = "C:\\Users\\ahopyar\\Desktop\\kk.txt";
static List<User> users = new ArrayList<>();
public static void main(String[] args) throws EncryptedDocumentException, InvalidFormatException, IOException {
readExcell();
}
private static void readExcell() throws EncryptedDocumentException, InvalidFormatException, IOException {
File file = new File(KK_PATH);
System.out.println(file.exists());
}
}
公共类主{
私有静态字符串KK_PATH=“C:\\Users\\ahopyar\\Desktop\\KK.txt”;
私有静态字符串EXCELL_PATH=”C:\\Users\\ahopyar\\Desktop\\kk.txt”;
静态列表用户=新建ArrayList();
公共静态void main(字符串[]args)引发EncryptedDocumentException、InvalidFormatException、IOException{
readExcell();
}
私有静态void readExcell()引发EncryptedDocumentException、InvalidFormatException、IOException{
File File=新文件(KK_路径);
System.out.println(file.exists());
}
}
如果我运行此代码,它将打印“true”,但如果我更改File File=new File(KK_路径)代码>as文件文件=新文件(EXCELL\u路径)代码>打印错误
这是如何可能的,我如何修复它?(文件位于“C:\Users\ahopyar\Desktop\kk.txt”)在EXCELL\u路径的开头有一个不可见的“”字符。语法糖注意,即使在Windows平台上,您也可以使用C:/some/folder/file.txt
一个正斜杠,我发现它不像转义的双反斜杠那样容易出错,也更容易阅读。@Ivar我只是好奇你是怎么找到这个的,呵呵?当这样的字符串看起来完全相等时,我通常会在它前面选择一点,然后在字符串中按Shift+向右箭头键,看看它是否挂在某个特定的点上。这是某种看不见的性格的迹象通过删除整个字符串重写该字符串,然后手动键入,以确保不会再次复制粘贴相同的错误