Java IO 是否有最大文件名长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4512742/
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
Does Java IO have a maximum file name length?
提问by Dónal Boyle
Different operating systems have different file name max lengths. Does Java have any limit on file name length when working with files?
不同的操作系统有不同的文件名最大长度。处理文件时,Java 对文件名长度有任何限制吗?
采纳答案by Guillaume
Java has no maximum file name length, except obviously for the String max length limit (which is the array max length, i.e. Integer.MAX_VALUE). Maybe some JVMs have a lower limit but I never run into such a problem (and I'm almost certain it would be a bug with respect to Java specifications), certainly OSes can have one.
Java 没有最大文件名长度,显然除了字符串最大长度限制(即数组最大长度,即 Integer.MAX_VALUE)。也许一些 JVM 有一个较低的限制,但我从来没有遇到过这样的问题(我几乎可以肯定这将是 Java 规范方面的一个错误),当然操作系统可以有一个。
回答by Casey
Windows has a 256 character filename length. Unix has about the same I believe. So while the Java IO may not have a defined length (String length maybe for sure), it would be dependent on the implementation for the operating system.
Windows 的文件名长度为 256 个字符。我相信 Unix 也差不多。因此,虽然 Java IO 可能没有定义的长度(字符串长度可能是肯定的),但它取决于操作系统的实现。
回答by ungalcrys
I had made a test and got ~ 1150 chars max length.
我做了一个测试,得到了大约 1150 个字符的最大长度。
if (!destFile.exists()) {
try {
destFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else
return;
...
...
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
and got
并得到
java.io.FileNotFoundException: /media/34A0-486C/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50/51/52/53/54/55/56/57/58/59/60/61/62/63/64/65/66/67/68/69/70/71/72/73/74/75/76/77/78/79/80/81/82/83/84/85/86/87/88/89/90/91/92/93/94/95/96/97/98/99/100/101/102/103/104/105/106/107/108/109/110/111/112/113/114/115/116/117/118/119/120/121/122/123/124/125/126/127/128/129/130/131/132/133/134/135/136/137/138/139/140/141/142/143/144/145/146/147/148/149/150/151/152/153/154/155/156/157/158/159/160/161/162/163/164/165/166/167/168/169/170/171/172/173/174/175/176/177/178/179/180/181/182/183/184/185/186/187/188/189/190/191/192/193/194/195/196/197/198/199/200/201/202/203/204/205/206/207/208/209/210/211/212/213/214/215/216/217/218/219/220/221/222/223/224/225/226/227/228/229/230/231/232/233/234/235/236/237/238/239/240/241/242/243/244/245/246/247/248/249/250/251/252/253/254/255/256/257/258/259/260/261/262/263/264/265/266/267/268/269/270/271/272/273/274/275/276/277/278/279/280/281/282/283/284/285/286/287/288/289/290/291/292/293/294/295/296/297/298/299/300/301/302/303/304/305/306/307/308/309/310/0.mp3
(No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
at java.io.FileOutputStream.<init>(FileOutputStream.java:145)
This was done on a fat32 filesystem from linux.
这是在 linux 的 fat32 文件系统上完成的。
回答by Aubin
glibc doesn't impose a limit, see The GNU C Library, limits for files
glibc 不施加限制,请参阅GNU C 库,文件限制
Macro: int PATH_MAX
The uniform system limit (if any) for the length of an entire file name (that is, the argument given to system calls such as open), including the terminating null character.
Portability Note:The GNU C Library does not enforce this limit even if PATH_MAX is defined.
Macro: int FILENAME_MAX
The value of this macro is an integer constant expression that represents the maximum length of a file name string. It is defined in stdio.h.
Unlike PATH_MAX, this macro is defined even if there is no actual limit imposed. In such a case, its value is typically a very large number. This is always the case on GNU/Hurd systems.
Usage Note:Don't use FILENAME_MAX as the size of an array in which to store a file name! You can't possibly make an array that big! Use dynamic allocation (see Memory Allocation) instead.
宏:int PATH_MAX
整个文件名长度的统一系统限制(如果有)(即提供给系统调用如 open 的参数),包括终止空字符。
可移植性说明:即使定义了 PATH_MAX,GNU C 库也不会强制执行此限制。
宏:int FILENAME_MAX
该宏的值是一个整数常量表达式,表示文件名字符串的最大长度。它在 stdio.h 中定义。
与 PATH_MAX 不同,即使没有施加实际限制,也会定义此宏。在这种情况下,它的值通常是一个非常大的数字。在 GNU/Hurd 系统上总是如此。
使用注意事项:不要使用 FILENAME_MAX 作为存储文件名的数组大小!你不可能制作这么大的数组!改用动态分配(请参阅内存分配)。
So, Java has no limit - except the max length of a String - since the underlying system hasn't. On Windows, pathes may be prefixed by \\?\ to be unlimited.
因此,Java 没有限制——除了字符串的最大长度——因为底层系统没有。在 Windows 上,路径可能以 \\?\ 为前缀以不受限制。
回答by PeterMmm
Java may hit the maximal String length: String's Maximum length in Java - calling length() method
Java 可能达到最大字符串长度:Java 中字符串的最大长度 - 调用 length() 方法
回答by Peter Lawrey
Java needs to turn all filename strings into a byte[] to interact with the OS. This means for some character sets the limit will be less than 2 billion. However I don't know of any OS which supports file names of this length. ;)
Java 需要将所有文件名字符串转换为 byte[] 以与操作系统交互。这意味着对于某些字符集,限制将少于 20 亿。但是我不知道任何支持这种长度文件名的操作系统。;)
回答by kiwicptn
Since file name is a String and length and position methods return an int, I'd say the maximum is Integer.MAX_VALUE.
由于文件名是一个字符串并且长度和位置方法返回一个整数,我会说最大值是 Integer.MAX_VALUE。