Japan's appeal as an economy of stability and long-range growth has become a magnet for vast investments by the oil-rich countries of the Middle East.

Since early this year, billions of oil dollars have poured into Japanese securities and bank deposits, sending the yen upward and pushing the Tokyo stock exchange to new highs.

Japanese stocks, once all but ignored by foreigners because of their low yields, have become the new darlings of international banks and trading houses, many of them dealing for the central banks of members of the Organization of Petroleum Exporting Countries.

American and European pension funds also are turning to yen assets and bank deposits, adding to the deluge, according to sources in the Tokyo financial community.

But it is the flow of oil money that has astonished some of Tokyo's experienced investment analysts. Sumio Fukabori, general manager of Nomura Securities Co.'s institutional research department, says the investors are looking for long-term investments.

Just returned from a Middle East trip, Fukabori said he was amazed to find buyers eager to make investments that will be good for a hundred years. He said he has never encountered interest in such long periods before, and compares it to the enthusiasm investors once held for the long-term prospects of gold.

Fukabori, like others, recommends Japanese blue-chip stocks of well-managed companies mostly in high-technology fields such as electronics. That is where most of the oil money invested in stocks is going. Shares of companies such as Hitachi, Sony and Nippon Steel are particularly bright attractions, he said.

The trend to the yen began early this year but soared in earnest during midsummer, particularly on the stock market. Since the end of July, the Dow Jones averages of 225 stocks on the Tokyo exchange have climed more than 300 points, breaking through the 7,000 level early this month.

Oil money also has poured into Japanese government bonds and into deposit accounts, neither of which offer unusually high annual returns. But they are considered safe and many foreign customers turn to them believing that the yen is going up for some time to come and expecting to benefit from its appreciation.

It is a drastic turnaround for Japan, which has been accustomed to sending more investment money abroad than it took in from foreign customers.According to the Ministry of Finance, Japan suffered a net outflow of 131 billion yen (about $612 million) last year in stock investments alone. Then the tide turned and in August alone the net inflow of stock purchases topped 250 billion yen (about $1.1 billion). In the same month, the net inflow of capital into Japanese bonds was more than twice as great as the net outflow in all of 1979.

Analysts agree that the basic strength of the Japanese economy is the key factor in attracting the oil money. It survived the latest round of oil price increases without stumbling. Inflation is modest and growth rates of 4 to 5 percent are projected this year and next.

"It's this perception that Japan has weathered the second oil shock that means a lot [to foreign investors,"] said David Bussman, analyst for the Bank of America's Tokyo office.

Coupled with Japan's strength is the common perception of the U.S. dollar as a weakening currency that is going nowhere. The oil-producing countries in December began to diversify their portfolios and began looking beyond the dollar for something likely to appreciate. One analyst said a common rule of thumb is that the Japanese yen will appreciate at the rate of an average of 10 yen a year against the dollar for the next 10 years.

Another analyst, Citibank Vice President Raymond Soudah, said, however, it is not correct to assume that the oil countries are switching investments from dollars to yen assets on a large scale.

Rather, he said, they are keeping their old dollar investments but putting more of their new revenues into yen.

They don't want to keep all of their eggs in one basket, and the dollar has been weakening for the past five years," he said.

Soudah and other experts also think that the freezing of Iranian assets by by the U.S. when the hostage crisis erupted last year may also have been a factor in the decision by other Middle Eastern countries to go to the yen.

"There is the fear in the Middle East countries that 'this could happen to us, too,'" Soudah said.

No one knows exactly how much each country is pouring into Japan these days. Many of the transactions are managed for the central banks of oil-producing countries by large European banks. Kuwait is believed to be one of the largest investors simply because so many transactions lately have come through the Swiss Credit Bank, which handles Kuwaiti investments.

The dollar's decline this year has made a lot of the yen investments look prety good. One analyst cited information he said he had received about a big purchase of Japanese government bonds by Saudi Arabia last spring. Besides getting a 12 percent yield on the bonds themselves, he said, the Saudis also benefited in the currency exchange because the yen exchange rate has risen from 250 to 212 against the dollar since the purchase was made.

The Bank of America's Bussman estimates foreign-owned yen assets of all types have increased by about $18 billion since January, with Japanese securities -- blue-chip stocks and government bonds -- accounting for about one-third of that increase.

The increase in foreign bank deposits here has been about a billion dollars a month, he said. He believes that about 10 percent of the OPEC surplus funds now is going into Japanese yen assets, as compared with 2 or 3 percent before.

The heavy trading in Japanese stocks has raised some eyebrows about foreign ownership of Japanese companies, but no one is pushing any panic button. Ministry of Finance approval is required to permit foreign ownership of any company to exceed 25 percent. The ministry has raised no objections to any company exceeding that limit so far.

Some believe that the oil countries' investments in Japanese corporations are a way of getting close to Japanese accomplishments in high technology. And it may be just good politics as well as economics, some say. Japan, after all, is one of the Middle East's best oil customers, importing more than 70 percent of its oil from countries on the Persian Gulf.

"In a way, they are self-srving investments," observes Citibank's Soudah. "The Saudis can say, 'We are not just charging you $32 a barrel for our oil but we are also investing in your country.'"