|
9 | 9 | import com.github.javaparser.ast.stmt.*; |
10 | 10 |
|
11 | 11 |
|
12 | | -import java.io.BufferedReader; |
13 | | -import java.io.IOException; |
| 12 | +import java.io.*; |
| 13 | +import java.nio.charset.StandardCharsets; |
14 | 14 | import java.nio.file.Files; |
15 | 15 | import java.nio.file.Paths; |
16 | 16 | import java.util.Arrays; |
17 | 17 | import java.util.List; |
| 18 | +import java.util.stream.Stream; |
18 | 19 |
|
19 | 20 | import static com.github.javaparser.ParseStart.*; |
20 | 21 | import static com.github.javaparser.Providers.provider; |
@@ -48,25 +49,28 @@ private static <T extends Statement> void addBracket(CompilationUnit cu, Class<T |
48 | 49 |
|
49 | 50 | public static void main(String[] args) throws IOException { |
50 | 51 |
|
51 | | - StringBuilder sb = new StringBuilder(); |
52 | | - |
53 | 52 | String path = SourceFormat.class.getResource("/example.java").getPath(); |
54 | 53 | path = path.substring(1); |
55 | 54 |
|
56 | | - try (BufferedReader br = Files.newBufferedReader(Paths.get(path))) { |
57 | | - String line; |
58 | | - while ((line = br.readLine()) != null) { |
59 | | - sb.append(line).append("\n"); |
60 | | - } |
| 55 | + StringBuilder contentBuilder = new StringBuilder(); |
61 | 56 |
|
62 | | - } catch (IOException e) { |
| 57 | + try (Stream<String> stream = Files.lines(Paths.get(path), StandardCharsets.UTF_8)) { |
| 58 | + stream.forEach(s -> contentBuilder.append(s).append("\n")); |
| 59 | + } |
| 60 | + catch (IOException e) { |
63 | 61 | e.printStackTrace(); |
64 | 62 | } |
65 | 63 |
|
| 64 | + String output = ""; |
66 | 65 | try { |
67 | | - System.out.println(format(sb.toString())); |
| 66 | + output = format(contentBuilder.toString()); |
68 | 67 | } catch (SourceException e) { |
69 | 68 | e.printStackTrace(); |
70 | 69 | } |
| 70 | + |
| 71 | + FileWriter fileWriter = new FileWriter("generated/example.java"); |
| 72 | + fileWriter.write(output); |
| 73 | + fileWriter.close(); |
| 74 | + |
71 | 75 | } |
72 | 76 | } |
0 commit comments