【问题标题】:Java File mkdir gives false resultJava File mkdir 给出错误的结果
【发布时间】:2016-04-01 13:48:44
【问题描述】:

所以我正在尝试制作一个地图以将一些文件存储在程序开始时制作的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 来说没问题。我还检查了我是否可以写并且得到了真实的回复,但它仍然不会在testRun 中生成dir

【问题讨论】:

    标签: java windows file


    【解决方案1】:

    您不能使用:(冒号)创建文件夹或文件名

    详情:
    关于微软资源,以下字符是 Windows 保留的,不能用于创建文件夹或文件名:

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

    参考:

    【讨论】:

      猜你喜欢
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 2011-11-15
      • 2019-02-06
      • 2014-01-30
      • 2018-08-01
      相关资源
      最近更新 更多