I don't know much about rail specifically, just some of the economics that apply.

The quality of some of China's infrastructure can mostly be attributed to the quality that when government has an agenda, it is generally good at achieving that agenda. The China government has the agenda of beating every other country. Combined with its totalitarian control, this allows the government to succeed at the narrow goal of trains running on time.

I can't say anything specific about Japan's rail. One thing I can say is that when thinking in terms of economics, we don't want to look at specific examples so much as the economy as a whole. An example for why is that Japan's rail system could be better than others due to reasons that reduce economic dynamism. On the whole this would be bad even though it would make Japan look like it's doing good. Or maybe Japan's transportation system is a standout in the arena of free markets. I don't know.

The most common example that people tend to use for why they think government can do good is healthcare in the non-US world. They point at the benefits they get but miss the lack of dynamism this creates. As messed up as the US healthcare system is, it's still (excluding maybe Singapore) the freest and it has the most dynamism and countries that don't have those are being indirectly subsidized by the US healthcare system. Without the US healthcare system, the ones Europeans like would be at a crossroads between being more Soviet or less regulated by the government.