java SD卡上的目录中的文件数量有限制吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2651907/
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
Is there a limit for the number of files in a directory on an SD card?
提问by jamesh
I have a project written for Android devices. It generates a large number of files, each day. These are all text files and images. The app uses a database to reference these files.
我有一个为 Android 设备编写的项目。它每天生成大量文件。这些都是文本文件和图像。该应用程序使用数据库来引用这些文件。
The app is supposed to clear up these files after a little use (perhaps after a few days), but this process may or may not be working. This is not the subject of this question.
该应用程序应该在使用一段时间后(可能几天后)清除这些文件,但此过程可能会或可能不会起作用。这不是这个问题的主题。
Due to a historic accident, the organization of the files are somewhat naive: everything is in the same directory; a .hiddendirectory which contains a zero byte .nomediafile to prevent the MediaScanner indexing it.
由于历史事故,文件的组织有些幼稚:一切都在同一个目录中;一个.hidden包含零字节.nomedia文件的目录,以防止 MediaScanner 对其进行索引。
Today, I am seeing an error reported:
今天,我看到一个错误报告:
java.io.IOException: Cannot create: /sdcard/.hidden/file-4200.html
at java.io.File.createNewFile(File.java:1263)
Regarding the sdcard, I see it has plenty of storage left, but counting
关于 sdcard,我看到它有足够的存储空间,但还在计数
$ cd /Volumes/NO_NAME/.hidden
$ ls | wc -w
9058
Deleting a number of files seems to have allowed the file creation for today to proceed.
删除一些文件似乎允许今天的文件创建继续进行。
Regrettably, I did not try touching a new file to try and reproduce the error on a commandline; I also deleted several hundred files rather than a handful.
遗憾的是,我没有尝试touching 一个新文件来尝试在命令行上重现错误;我还删除了数百个文件而不是少数文件。
However, my question is:
但是,我的问题是:
- are there hard limits on filesize or number of files in a directory?
- am I even on the right track here?
- 文件大小或目录中的文件数量是否有硬限制?
- 我是否在正确的轨道上?
Nota Bene: The SD card is as-is - i.e. I haven't formatted it, so I would guess it would be a FAT-* format.
Nota Bene:SD 卡是原样 - 即我没有格式化它,所以我猜它会是 FAT-* 格式。
The FAT-32 format has hard limits of filesize of 2GB (well above the filesizes I am dealing with) and a limit of number of files in the root directory. I am definitely not writing files in the root directory.
FAT-32 格式的文件大小硬限制为 2GB(远高于我正在处理的文件大小)和根目录中文件数量的限制。我绝对不会在根目录中写入文件。
回答by Dave Webb
There's a limit on 512 entries in the root directory of FAT filesystem. This limit comes about because the root directory lives in a particular place on FAT filesystems.
FAT 文件系统的根目录中有 512 个条目的限制。出现此限制是因为根目录位于 FAT 文件系统上的特定位置。
For other directories this limit is not in place. Additionally, FAT32 removed the 512 entry limit for the root filesystem by treating the root directory the same as any other directory.
对于其他目录,此限制不存在。此外,FAT32 通过将根目录与任何其他目录一样对待,删除了根文件系统的 512 条目限制。
Using long filenames - i.e. not in 8.3 format - means than a single file uses multiple directory entries.
使用长文件名——即不是 8.3 格式——意味着单个文件使用多个目录条目。
Some Googling finds some people claiming that a FAT32 directory can have a maximum of 65,536 entries(which would be fewer files if they had long file names). However, none of the sources that mentioned this limit seemed that reliable so I thought I'd test this.
一些谷歌搜索发现有些人声称 FAT32 目录最多可以有 65,536 个条目(如果文件名很长,文件会更少)。然而,提到这个限制的消息来源似乎都不可靠,所以我想我会测试一下。
I wrote a script which creates files with 30 character filenames, meaning each file would need 4 directory entries. When the script got to file 16,384 it failed with an IO error and I couldn't create more files in my test directoy. So this does seem to validate the 65,536 entry limit.
我编写了一个脚本来创建具有 30 个字符文件名的文件,这意味着每个文件需要 4 个目录条目。当脚本到达文件 16,384 时,它因 IO 错误而失败,我无法在我的测试目录中创建更多文件。所以这似乎确实验证了 65,536 个条目限制。
If you're hitting this limit at 9,000 files then your files must be using at least 7 entries each which corresponds to filenames that are at least 66 characters long. Does this match what you're doing? (Or you could have some short filenames and some very, very long ones, but you get the idea.)
如果您达到 9,000 个文件的限制,那么您的文件必须至少使用 7 个条目,每个条目对应于至少 66 个字符长的文件名。这与您正在做的事情相符吗?(或者你可以有一些短的文件名和一些非常非常长的文件名,但你明白了。)
回答by Victor
I think the limit of files in a directory in Fat32 also depends on the length of the filenames
我认为 Fat32 目录中文件的限制也取决于文件名的长度
http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
回答by OferR
I have done some more testing ( reliable testing :-) ), trying to write 100,000 short-named directories in a single directory. The limit was reached at 65,536.
我做了一些更多的测试(可靠的测试:-)),试图在一个目录中写入 100,000 个短命名的目录。上限达到了 65,536。
The test was done on Nexus One running Android 2.2, but I believe the result holds for any FAT32 SD card.
测试是在运行 Android 2.2 的 Nexus One 上完成的,但我相信结果适用于任何 FAT32 SD 卡。
回答by flaja
There is a practical limitmuch lower than 512,
depending on the Android deviceyou connect :
When I connect my phone (Samsung A5 2015) to my computer (Ubuntu) :
from my computer, I can easily read/write files on my phone,
except for a directory which contains 300 files.
It is infinitely slow for any simple action :
list the content, copy a file, delete a file . . .
So, I think that there is a practical limit to the number of files for my phone.
I have also tablets with which I do not have this problem.
这里有一个实际的限制要比512低,
这取决于在Android设备上您连接:
当我我的手机(三星A5 2015)连接到我的电脑(Ubuntu的):从我的电脑,我可以轻松读取/我的手机上写文件,
除了一个包含 300 个文件的目录。任何简单的操作都非常慢:列出内容、复制文件、删除文件。. . 所以,我认为我的手机的文件数量是有实际限制的。我也有平板电脑,我没有这个问题。

