Java 文件名、目录名或卷标语法不正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24116311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The filename, directory name or volume label syntax incorrect
提问by user3721625
I get the following exception:
我收到以下异常:
D:\Try\folder\filename_1_06_09_2014_12:46:18.txt (The filename, directory name, or volume label syntax is incorrect)
D:\Try\folder\filename_1_06_09_2014_12:46:18.txt(文件名、目录名或卷标语法不正确)
While using timestamp in renaming filename here:
在此处重命名文件名时使用时间戳:
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"));
}
I can't figure it out where I use invalid characters in windows filename. Any suggestions please?
我无法弄清楚在 Windows 文件名中使用无效字符的位置。请问有什么建议吗?
采纳答案by NcDreamy
You cannot use colons :
to create file in Windows. If you absolutely need the timestamp then you could try to add underscores for them as well.
:
在 Windows 中不能使用冒号创建文件。如果您绝对需要时间戳,那么您也可以尝试为它们添加下划线。
回答by T.J. Crowder
You can't have colons in filenames on Windows file systems, they can only be used to separate drives from the rest of the path:
Windows 文件系统上的文件名中不能有冒号,它们只能用于将驱动器与路径的其余部分分开:
D:\Try\folder\filename_1_06_09_2014_12:46:18.txt Issue is here ------------------------^--^
回答by Samvel Kartashyan
Just change root directory name with valid charachters
只需使用有效字符更改根目录名称
回答by ctorrillas
This problem can also be risen because of the permissions at level security you have at the folder you are trying to open or file you are trying to write.
由于您在尝试打开的文件夹或尝试写入的文件中拥有级别安全性的权限,此问题也可能会出现。