I am working on creating my own Custom file parser (it can read & write a .yml file or any other custom text file)
Right now I am just working on reading a pre-written file. (Yes i have looked at several other similar issues. these didn't seem to even get close to what i'm looking for as i don't want to use an external library. [a link] (How do I parse a YAML file?) )
However, the code I have currently does not seem to be returning the correct results. It keeps returning an empty string, implying that the file didn't contain the values i asked for.
protected String readValue(String value) {
BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getResource("Locale.txt")));
boolean isMultiline = false;
String multiLineCombo = "";
for (String s : reader.lines().toArray(String[]::new)) {
if (s.startsWith(Pattern.compile("[^a-zA-Z0-9]") + "")) {
continue;
}
if (s.trim().startsWith(Pattern.quote("(?i)") + value + ":")) {
if (s.contains(": |-")) {
isMultiline = true;
continue;
}
if (isMultiline) {
if (!s.contains(":")) {
multiLineCombo = multiLineCombo.concat(s + "\n");
continue;
} else {
isMultiline = false;
return multiLineCombo;
}
}
try {
reader.close();
} catch (Exception e) {
}
return s.substring(s.indexOf(":"));
}
}
return "";
}
I'm looking for it to read simple values constructed like so:
# comments...
OwnerName: 'something here'
# more comments
# .
# yeet...
ListOfNames:
- Ann
- Brent
- Crumpled Bananas
- more values
# multiline strings like this
MultipleLines: |-
Line one
Line two
and line three
Yes, I have written the values i'm trying to pull in the target file, the values are in there, i'm just reading them wrongly.
If anybody has some tips or can help me as to what i'm doing wrong here, that would be awesome!
Edit: Seems i need to explain why i'm attempting to create my own file reader instead of using a third-party such as SnakeYaml... Mainly because i do not want to specifically parse only YAML files, I'm looking for this as more of my own custom data storage system. The library i'm working with has a built-in yaml reader/writer, but it ignores comments, deleting them and removing any white spaces and formatting i add in. Additionally, I do not want this to be overly dependent on whether said java application has the specified libraries available. (depending on whether it has access to the Internet or not to download resources) As such, i want to create my own, both to negate having to depend on external libraries and to increase reliability and customize-ability.