Java文件mkdir给出错误的结果

时间:2016-04-01 13:48:44

标签: java windows file

所以我试图制作一张地图,将一些文件存储在程序开头的dir中。但是在Windows上仍然存在问题,因为目录永远不会发生。我无法找到解决方案。相同的代码在Unix系统上运行良好,但在Windows系统上运行不正确。

protected String createScreenshotMap(){
        this.dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        this.date = new Date();
        String testMap = this.checkOS();
        File fileMap = null;
        boolean check;
        try{
            if(os.indexOf("win") >= 0){
                fileMap = new File(testMap);
                check = fileMap.canWrite();
                System.out.println(check);
                String path = "C:" + File.separator + "testRun" + 
                                     File.separator + this.date.toString();
                fileMap = new File(path);
                System.out.println(fileMap.getAbsolutePath());
            }else{
                fileMap = new File(testMap + this.date.toString() + "/");
            }
            check = fileMap.mkdir();
            System.out.println(check);
        }catch (Exception e){
            e.printStackTrace();
        }
        return testMap;
    }

如果我运行这段代码,我会得到以下输出

true
C:\testRun\Fri Apr 01 15:30:47 CEST 2016
false

我还检查过我testRun存在,这对java来说没问题。我还检查了我是否可以写作并且回复了,但它仍然无法在dir中生成testRun

1 个答案:

答案 0 :(得分:7)

您无法使用:(冒号)

创建文件夹或文件名

<强>详细信息:
关于Microsoft资源,Windows保留以下字符,不能用于创建文件夹或文件名:

< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

参考: