文件名,目录名或卷标语法不正确

use*_*625 10 java

我得到以下异常:

D:\ Try\folder\filename_1_06_09_2014_12:46:18.txt(文件名,目录名或卷标语法不正确)

在此处使用时间戳重命名文件名时:

for (int i = 0; i < children.length; i++) { 
    String fileName = new SimpleDateFormat("MM_dd_yyyy_hh:mm:ss").format(new Date());
    copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, files+"_"+fileName+".txt"));
}
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚我在Windows文件名中使用无效字符的位置.有什么建议吗?

小智 22

您不能使用冒号:在Windows中创建文件.如果您绝对需要时间戳,那么您也可以尝试为它们添加下划线.

  • 这是我的问题。我将格式更改为以下格式,现在我的屏幕截图在 Windows 目录中正确命名: java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'_'HHmmss'_'"); (2认同)
  • 节省了我的时间...正在搜索所有其他地方,但这也是我问题的根本原因...非常感谢:) (2认同)

T.J*_*der 9

您不能在Windows文件系统上的文件名中使用冒号,它们只能用于将驱动器与路径的其余部分分开:

D:\Try\folder\filename_1_06_09_2014_12:46:18.txt 
Issue is here ------------------------^--^