Using a sample of about 1.46 million hadronic Z decays collected between 1991 and 1993 with the ALEPH detector at LEP, the energy distribution of the B0 and B± mesons produced at the Z resonance is measured by reconstructing semileptonic decays B → ℓνℓD(X) or . The charmed mesons are reconstructed through the decay modes D0 → K−π+, D0 → K−π+π−π+, D+ → K−π+π+ and . The neutrino energy is estimated from the missing energy in the lepton hemisphere. Accounting for B∗ and B∗∗ production, the shape of the scaled energy distribution xE(b) for mesons containing a b quark is compared to the predictions of different fragmentation models. The mean value of xE(b) is found to be 〈xE(b)〉 = 0.715 ± 0.007(stat) ± 0.013(syst).