Maintenance is done at night and, when that’s not happening, they run freight trains. In Tokyo, last train used to be like 1:30ish on some lines and started up again a bit before 5am. Now it’s more like a bit after midnight and starting again closer to 5am. JR is a private company (a group of them, actually) and they keep squeezing things further, presumably for more profit.
They did start a bus line between nightlife areas a few years ago, but it never got popular and was closed (and also just took drunk people around from one party area to the next and not home).