解释了特殊字符但不是实际文件名的一部分 - 导致异常

Special Characters interpreted but not part of the actual file name - resulting in exception

我目前正在一个文件夹位置创建一个文件,另一个进程读取这个文件的内容,但是即使创建的文件没有任何特殊字符,我现在也会因为这两个特殊字符而抛出 FileNotFoundException ~$,但是这些特殊字符不是物理文件名的一部分 "verifysinglenumber_20210208203905995.xls" 这意味着它们不作为文件的一部分存在,甚至没有以这些字符为前缀

创建文件时注意那些特殊字符

刚刚创建的新文件 D:\Java\IdeaProjects - 单号 - BAU\Selenium\src\test\resources\path 个文件\ : ~$verifysinglenumber_20210208203905995.xls

这是例外情况,请注意那些特殊字符 ~$

java.io.FileNotFoundException: D:\Java\IdeaProjects - 单号 - BAU\Selenium\src\test\resources\path 个文件~$verifysinglenumber_20210208203905995.xls (系统找不到指定的文件)

字符串轮询路径和资源路径只是为位置构建字符串

public XSSFSheet GetExcelSheet(String workbookName, String sheetName, String sPollingPath) throws Exception {
    excelFile = new File(resourceFolderPath + sPollingPath + workbookName);
    try
    {
        fileInputStream = new FileInputStream(excelFile);
    }catch(Exception e){
        System.out.println(e.getMessage().toString());
    }

我也在使用 JNotifier,在创建新 excel 文件时监听文件夹。

你们能帮我解释一下为什么文件用这些特殊字符解释,即使它们根本不存在吗?

我已决定按照建议使用监视服务 API 并使用此解决方案更新此答案。

 public static void watchFolderPickup() throws Exception {
WatchService watchyFier = FileSystems.getDefault().newWatchService();

Path pickupLocation = Paths.get("C:\Users\localUser\Documents\IdeaProjects - Single Number - BAU\Selenium\src\test\resources\path files\");
WatchKey directoryWatchKey = pickupLocation.register(watchyFier, StandardWatchEventKinds.ENTRY_CREATE);



while (true)
    for(WatchEvent<?> event : directoryWatchKey.pollEvents()){
        WatchEvent<Path> ev = cast(event);
        Path fileName = ev.context();
        String fileInLocation = fileName.getFileName().toString();

        BAUFileNameSetter setFilename =  new BAUFileNameSetter();
        setFilename.setFileName(fileInLocation);
        dataSetfilename = setFilename.getFileName();
        System.out.println("File Created for New Request");
        BaseTest.setup();
        return;
  
    }
}