U.S. railroads are back, with freight volume posting the first quarterly gain in two years.

Carloads increased 2.8% in the fourth quarter from a year earlier, with only a few days remaining in the period, according to American Association of Railroads weekly reports compiled by Susquehanna Financial Group. Consumer spending fueled the gain as people bought more goods instead of spending on dining out or attending concerts during the coronavirus pandemic. That offset lower volume for oil, coal, autos and other products.

Intermodal cargo—items shipped in containers that move by ship, rail and truck—is leading the rebound, rising slightly more than 10%. The surge reflects stronger demand for electronics, patio furniture and exercise equipment as consumers are stuck at home.

The recovery is a welcome development for an industry that was struggling even before the coronavirus pandemic took hold early this year. Volume was weak as fossil fuel production slowed and growth in consumer spending was weighted toward services such as entertainment and dining out.

Railroad revenue will likely drop this quarter because carriers can’t charge as much for intermodal, which is handed off to trucks for final delivery, as they can for other freight. Still, efficiency gains and cost cuts at CSX Corp. and Union Pacific Corp. will probably push their profits higher.

The carload rebound will spread to other goods—such as oil, autos, metals and sand used for fracking—as the pandemic gets under control, allowing normal activities to resume, said Lee Klaskow, an analyst at Bloomberg Intelligence. Volume at North American railroads, which includes Canada’s two largest, could increase by a percentage in the mid-single digits next year, he said.

Meanwhile, a trucking market that is working at capacity will likely help the rails keep pricing strong. CSX and Union Pacific see opportunities to take some cargo from the highways, touting improved on-time performance and the fuel efficiency of railroads over 18-wheelers.

Investors have already anticipated the recovery. All companies in a Standard & Poor’s index of U.S. railroads have climbed this year, led by Kansas City Southern’s 31% advance through Monday. Canadian Pacific Railway Ltd. and Canadian National Railway Co. have also gained.