Java 如何将文件字符串格式化为点路径
我想做一个配置来存储项目,但是,当我创建路径来获取值时,发生了一些错误Java 如何将文件字符串格式化为点路径,java,Java,我想做一个配置来存储项目,但是,当我创建路径来获取值时,发生了一些错误 HashMap<String, Text> sections; private void loadKeys() { List<String> list = new ArrayList<>(); for (String s : sections.keySet()) { Text te = sections.get(s); String cha
HashMap<String, Text> sections;
private void loadKeys() {
List<String> list = new ArrayList<>();
for (String s : sections.keySet()) {
Text te = sections.get(s);
String changeable = s.substring(0, s.length() - 1);
for (int i = 0; i < te.lines(); i++) {
String line = te.getLine(i);
while (line.startsWith(" ")) {
line = line.substring(2);
}
if (!line.startsWith("-")) {
if (line.endsWith(":")) {
changeable = changeable + "." + line.substring(0, line.length() - 1);
} else {
list.add(changeable + "." + line);
}
}
}
}
for (String s : list) {
System.out.println(s);
}
}
我想要的输出:
- Test11.Test12.Test13:“测试”
- Test11.Test12.Test14:'test2'
- Test11.Test15:teste
- Test11.Test16.Test17:“测试”
- Test11.Test12.Test13:“测试”
- Test11.Test12.Test14:'test2'
- Test11.Test12.Test15:teste
- Test11.Test12.Test16.Test17:“测试”
正在重复测试12。你能帮我得到我想要的吗?提前谢谢这很容易。您只需保持当前的级别深度和级别名称。您可以通过递归或使用队列来实现
publicstaticmap readProperties(路径路径)引发IOException{
最后一级{
私有最终字符串名;
私人最终积分;
公共级别(字符串名称、整数){
this.name=名称;
这个。offs=offs;
}
}
Map Map=newlinkedhashmap();
//包含当前级别的所有根项目及其偏移量,以检测当前级别是否为子级别或父级别
Deque levels=新的LinkedList();
Pattern Pattern=Pattern.compile((?\\s*)(?[^::]+)\\s*:\\s*(?*)\\s*”;
文件.行(路径)
.map(模式::匹配器)
.filter(Matcher::matches)
.forEach(匹配器->{
int offs=matcher.group(“offs”).length();
//删除父级,直到达到当前级别的父级
而(!levels.isEmpty()&&levels.peekLast().offs>=offs){
levels.removeLast();
}
字符串key=matcher.group(“key”);
字符串值=matcher.group(“值”);
if(value.isEmpty())
级别。添加(新级别(关键,关闭));
其他的
map.put(levels.stream().map(level->level.name).collect(collector.joining(“.”)+“.”+键,值);
});
返回图;
}
看起来您可能需要学习使用调试器。请随便吃点。如果您以后仍有问题,请将您的问题更具体地描述为您需要的帮助。什么是文本
?您能给出完整的代码吗?什么是章节
?对不起,编辑并添加了Text.java和章节
public class Text {
private List<String> lines = new ArrayList<>();
public Text(String txt) {
if (txt.contains("\n")) {
for (String s : txt.split("\n")) {
lines.add(s);
}
} else {
lines.add(txt);
}
}
public int lines() {
return lines.size();
}
public String getLine(int line) {
return lines.get(line);
}
@Override
public String toString() {
String string = "";
for (String s : lines) {
if (string.equals("")) {
string = s;
} else {
string = string + "\n" + s;
}
}
return string;
}
}
Test11:
Test12:
Test13: 'test'
Test14: 'test2'
Test15: teste
Test16:
Test17: "test test"
public static Map<String, String> readProperties(Path path) throws IOException {
final class Level {
private final String name;
private final int offs;
public Level(String name, int offs) {
this.name = name;
this.offs = offs;
}
}
Map<String, String> map = new LinkedHashMap<>();
// contains all root items for current one with it's offset, to detecl that current level is sub level or parent
Deque<Level> levels = new LinkedList<>();
Pattern pattern = Pattern.compile("(?<offs>\\s*)(?<key>[^:]+)\\s*:\\s*(?<value>.*)\\s*");
Files.lines(path)
.map(pattern::matcher)
.filter(Matcher::matches)
.forEach(matcher -> {
int offs = matcher.group("offs").length();
// remove parent levels until reach the parent of current level
while (!levels.isEmpty() && levels.peekLast().offs >= offs) {
levels.removeLast();
}
String key = matcher.group("key");
String value = matcher.group("value");
if (value.isEmpty())
levels.add(new Level(key, offs));
else
map.put(levels.stream().map(level -> level.name).collect(Collectors.joining(".")) + '.' + key, value);
});
return map;
}