1 | // Written without an IDE!
|
2 |
|
3 | import java.util.*;
|
4 | import java.io.*;
|
5 |
|
6 | public class main {
|
7 | public static void main(String[] args) throws IOException {
|
8 | String s = readTextFile("input/main.java", "");
|
9 | System.out.println(splitAtBrackets(s));
|
10 | }
|
11 |
|
12 | static List<String> splitAtBrackets(String s) {
|
13 | List<String> list = new ArrayList<String>();
|
14 | int lastI = 0;
|
15 | for (int i = 0; i < s.length(); i++) {
|
16 | boolean a = i == 0 || "{}()[]".indexOf(s.charAt(i-1)) >= 0;
|
17 | boolean b = "{}()[]".indexOf(s.charAt(i)) >= 0;
|
18 | if (a || b) {
|
19 | // split
|
20 | if (lastI != i)
|
21 | list.add(s.substring(lastI, i));
|
22 | lastI = i;
|
23 | }
|
24 | }
|
25 | if (s.length() > lastI)
|
26 | list.add(s.substring(lastI));
|
27 | // done
|
28 | return list;
|
29 | }
|
30 |
|
31 | public static String readTextFile(String fileName, String defaultContents) throws IOException {
|
32 | if (!new java.io.File(fileName).exists())
|
33 | return defaultContents;
|
34 |
|
35 | FileInputStream fileInputStream = new FileInputStream(fileName);
|
36 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
|
37 | return loadTextFile(inputStreamReader);
|
38 | }
|
39 |
|
40 | public static String loadTextFile(Reader reader) throws IOException {
|
41 | StringBuilder builder = new StringBuilder();
|
42 | try {
|
43 | BufferedReader bufferedReader = new BufferedReader(reader);
|
44 | String line;
|
45 | while ((line = bufferedReader.readLine()) != null)
|
46 | builder.append(line).append('\n');
|
47 | } finally {
|
48 | reader.close();
|
49 | }
|
50 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
|
51 | }
|
52 |
|
53 | } |