So, the whole block is read into the memory when Bitcoin accesses it?
I believe so.
How does Bitcoin lookup a transaction from a block file?
Bitcoin Core normally does not need to lookup transactions. It can only do that if you enable the transaction index (
-txindex CLI option) in which case it does basically the same thing as for blocks, it makes the database and has each value point to the location in a file of where that transaction is located.
For transactions that are in your own wallet, it stores the transaction data in your wallet.dat file.
The databases also include other things like the current chainstate and the UTXO set.