0

I generate .java files from a PrintWriter class. These generated files contain many nested for loops. To properly indent everything, I am currently using something like:

for(int j = 0; j < i + 2; j++)
{
    pw.print("    ");
}
pw.println("{");

Just to properly indent the single parentheses.

Obviously, I could just make a method to do this. I'm more wondering if there's a library that will handle indention and such of outputted code.

3
  • What language is your output? Commented Jul 20, 2011 at 3:05
  • @Mark Elliot he said Java: I generate .java files from a PrintWriter class. Commented Jul 20, 2011 at 3:32
  • @Nivas: I guess my reading goggles (read: blinders) were on. Commented Jul 20, 2011 at 3:37

3 Answers 3

1

I think you might be better off not doing that in your code ( I dont know of any library that formats PrintWriter.out statements ) and instead relying on some formatters after your Java program has completed generating your .java files.

Here's some formatters

http://mindprod.com/jgloss/beautifier.html

Jacobe -- is command-line driven and the personal edition is free.

Sign up to request clarification or add additional context in comments.

1 Comment

This looks like a more perfect solution than that for which I asked; thank you very much!
1

you can either use javaparser to parse the output generated after parsing will be with standard indentation

1 Comment

Can you provide an example?
0

With Javaparser printing a source code (with default pretty printer) is as simple as that.

CompilationUnit cu = StaticJavaParser.parse(<path to source file>);
System.ou.println(cu.toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.