Why did Japan open its ports to European trade after centuries of isolationism?
They had little choice. Admiral Perry sailed into Tokyo Bay on July 8, 1853 with several ships and refused to leave until an agreement was reached.
The Japanese Government was in disarray at the time. There was no effective military counter move available to them. The Japanese had banned gunpowder weapons in the 1600s and although some were still available in Arsenals there was no ongoing development of gunpowder weapons. The American ships completely outclassed anything that the Japanese had on land or sea.
The Japanese were also aware of the defeats suffered by the Chinese at European hands. The Japanese stalled but could not avoid a Treaty the next year.
As a consequence the Japanese began a campaign of modernization and by the turn of the century were merrily exploiting the Chinese just like the Americans and the Europeans.