In this study, hierarchically porous carbon nanofibers (HPCNFs) containing numerous electroactive heteroatoms are prepared by controlling carbonization and chemical activation. The HPCNFs have a large surface area of 2862.1 m2 g−1, hierarchical porous structure, and 20.6 at% electroactive oxygen atoms. These HPCNFs exhibit excellent electrochemical performance, and their thermal treatment with melamine results in nitrogen-doped HPCNFs (N-HPCNFs) with 9.1 at% nitrogen and improved electrical property (1.8 × 102 Ω □−1). The electrochemical performance of N-HPCNFs is better than that of HPCNFs, displaying specific energy of 113 Wh kg−1, specific power of 105 kW kg−1, and stable cycle life of over 5000 cycles.