转载......
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class FormattedJavaCode {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage:java FormattedJavaCode ");
System.exit(1);
}
File sourceFile = new File(args[0]);
if (!sourceFile.exists()) {
System.out.println("Source file " + args[0] + " not exits");
System.exit(2);
}
StringBuilder sb = new StringBuilder();
Scanner input = new Scanner(sourceFile);
while (input.hasNext()) {
String str = input.nextLine();
String str1 = str.trim();
if (str1.length() > 0 && str1.charAt(0) == '{') {
sb.append("{");
if (str1.length() > 1)
sb.append("\r\n" + str.replace('{', ' '));
} else
sb.append("\r\n" + str);
}
input.close();
PrintWriter output = new PrintWriter(sourceFile);
output.print(sb.toString());
output.close();
}
}