简体   繁体   中英

How does FileInputStream find the file?

I'm designing a program that needs to read a file from a folder that contains roughly 10^8 files.

How does FileInputStream find the desired file from it's provided filename? Does it work similarly to a hashmap with O(1) lookup time, or does it linearly traverse the files in the given folder until it finds a match?

I imagine this might have more to do with how windows file storage works than with FileInputStream, but I'm honestly not sure.

The file name is passed to the OS, and it reads the directory looking for a matching name. It might optimise the lookup but Java doesn't get involved.

You might consider breaking the files into multiple directories and ideally using less files. Opening and closing lots of small files spends most of it's time opening and closing the file descriptor. The finding and reading is usually much smaller.

It asks the operating system to find the file. How does the operating system do it? It depends on the OS, and on the file system. In at least some cases, the answer is, "Yes, it works like a HashMap." On the other hand, I know of at least some OS/filesystem combinations that get seriously bogged down once you have more than a few thousand files in the same directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM