IDEs usually have a feature or set of features that can do this for you. E.g. Intellij IDEA comes with a nice and highly configurable auto formatting tool. Netbeans (Sun's free IDE) and Eclipse (IBM's free IDE) have probably similar features. Even some text editor like emacs support auto formatting.
Bottom line:
Get an IDE, use the relevant feature.