YamlConfiguration extends FileConfiguration { protected static String parseHeader(final String input) { final String[] lines = input.split("\r?\n", -1); final StringBuilder result = new StringBuilder(); boolean readingHeader = true; boolean foundHeader = false; String commentPrefixTrimmed = Commentable.COMMENT_PREFIX.trim(); for (int lineindex = 0; lineindex < lines.length && readingHeader; lineindex++) { final String line = lines[lineindex]; if (line.startsWith(commentPrefixTrimmed)) { if (lineindex > 0) { result.append('\n'); } if (line.length() > commentPrefixTrimmed.length()) { result.append(line.trim()); } foundHeader = true; } else if (foundHeader && line.isEmpty()) { result.append('\n'); } else if (foundHeader) { readingHeader = false; } } return result.toString(); }  static YamlConfiguration loadConfiguration(final File file); static YamlConfiguration loadConfiguration(final InputStream stream); static YamlConfiguration loadConfiguration(final Reader reader); @Override String saveToString(); @Override void loadFromString(final String contents); @Override YamlConfigurationOptions options();  }
@Test void parseHeader() { final String content = "# test123\n" + "test:\n" + " number: 5\n" + " string: Hello world\n" + " boolean: true\n" + " list:\n" + " - Each\n" + " - word\n" + " - will\n" + " - be\n" + " - in\n" + " - a\n" + " - separated\n" + " - entry\n" + "math:\n" + " pi: 3.141592653589793\n" + "timestamp:\n" + " canonicalDate: 2020-07-04T13:18:04.458Z\n" + " formattedDate: 04/07/2020 15:18:04\n"; MatcherAssert.assertThat( "Couldn't parse the header of the content!", YamlConfiguration.parseHeader(content), new IsEqual<>("# test123") ); }