You can't beat the ferry if cost is the overriding concern. You can travel for �44 each way, provided you're prepared to put up with an airline-style seat rather than a cabin on board. Grimaldi reservations
here.
The cheapest rail fare I can find is about �115 one-way, which gets you a standard class seat from Rome to Milan and then a bed in a four-berth sleeper from MIlan to Barcelona. Rail Europe is
here.
There are lots of variations � a variety of cabin grades on board the ferry, and expensive single/double sleepers with private shower on the train. So it depends what your priorities are!