我想自动格式化以下内容:
main[
key1=value1
key2=value2
structure1=type1[
key3=value3
key4=value4
]
structure4=type2[
key5=value5
key6=value6
]
list=[elementType[
key7=value7
key8=value8
], elementType[
key9=value9
key10=value10
]]
]
我的期望是这样的:
main[
key1=value1
key2=value2
structure1=type1[
key3=value3
key4=value4
]
structure4=type2[
key5=value5
key6=value6
]
list=[
elementType[
key7=value7
key8=value8
],
elementType[
key9=value9
key10=value10
]
]
]
原始来源非常庞大。我想这样做一次,因此我不需要通用解决方案。
有谁知道这样的自动格式化程序?谢谢!
答案 0 :(得分:0)
我为此创建了一个Java程序:
import java.io.*;
import java.util.*;
public class Formatter {
public static void main(String args[]) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java Formatter filename");
} else {
List<String> lines = readFile(args[0]);
int indent = 0;
for (String line: lines) {
String lineExtended = "x" + line + "x";
indent -= (lineExtended.split("\\]").length - 1);
for (int i = 0; i < indent; i++) {
System.out.print("\t");
}
System.out.println(line.trim());
indent += (lineExtended.split("\\[").length - 1);
}
}
}
private static List<String> readFile(String filename) throws Exception {
List<String> records = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
records.add(line);
}
reader.close();
return records;
}
}
但如果有更好的解决方案,我真的很好奇。
答案 1 :(得分:0)