Nanoscale control of charge doping in two-dimensional (2D) materials permits the realization of electronic analogs of optical phenomena, relativistic physics at low energies, and technologically promising nanoelectronics. Electrostatic gating and chemical doping are the two most common methods to achieve local control of such doping. However, these approaches suffer from complicated fabrication processes that introduce contamination, change material properties irreversibly, and lack flexible pattern control. Here we demonstrate a clean, simple, and reversible technique that permits writing, reading, and erasing of doping patterns for 2D materials at the nanometer scale. We accomplish this by employing a graphene/boron nitride (BN) heterostructure that is equipped with a bottom gate electrode. By using electron transport and scanning tunneling microscopy (STM), we demonstrate that spatial control of charge doping can be realized with the application of either light or STM tip voltage excitations in conjunction with a gate electric field. Our straightforward and novel technique provides a new path towards on-demand graphene pn junctions and ultra-thin memory devices.