Why does Bitcoin store keys in a database? According to this Bitcoin stores the user's keys and some additional data in a file called wallet.dat, which is a Berkley DB. Let's suppose the wallet.dat contained only the user's keys. Let's also suppose we're using linux. 

What is then the main reason for Bitcoin to store keys in a database? Wouldn't it have the same effect if keys were stored as flat files: the public key would be the filename and the private key would be the content of the file? 

Even if we had millions of keys, and therefore millions of files in a single directory, the i-node limit (maximum number of files in a directory) and file lookup speed shouldn't be a problem. The majority of linux systems use the ext4 as its filesystem and according to this the number of i-nodes in ext4 is unlimited (limited only by the disk memory). Also according to this the EXT4 uses a structure called HTree, which allows for file lookups to have a constant time complexity.
Historically bdb was used for the UTXO as well, so you could justify using it for the wallet as well for consistency. It makes absolutely no sense today, other than that it will always need to be supported to some extent for compatibility. 

Really the wallet could be replaced with any key value store at all and it would function completely fine. You would not want a single key per file because this tends to be incredibly inefficient and difficult to deal with from a filesystem, and a human interaction perspective. It's easier to back up a single file than tens of thousands in a directory.