# Example: a sectioned config file

> A complete custom SettingsFileFormat that flattens a config file with section headers into dotted keys, so its settings sync one by one.

URL: https://xichil.dev/docs/settings-saver/v1/examples/sectioned-format/ · Updated: 2026-11-16 · Applies to: Fabric, Quilt, Legacy Fabric, Forge, NeoForge, MC 1.7.10 - 26.3

A mod whose config has `[section]` headers. A custom format reads it into dotted keys such as
`video.fov`, so Settings Saver syncs, merges and excludes each setting on its own. The format uses only
the JDK.

```text title="config/mymod.cfg"
# My Mod settings
language = en

[video]
fov = 90
zoom = 150

[window]
x = 120
y = 80
```

## The format

```java title="src/main/java/com/example/mymod/compat/SectionedFormat.java"
package com.example.mymod.compat;

import dev.xichil.settingssaver.api.v1.SettingsFileFormat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * A file with [section] headers and "key = value" lines, read into section.key -> value.
 * Stateless: one instance serves every file.
 */
public final class SectionedFormat implements SettingsFileFormat {
    public static final SectionedFormat INSTANCE = new SectionedFormat();

    private SectionedFormat() {
    }

    @Override
    public Map<String, String> read(Reader reader) throws IOException {
        Map<String, String> values = new LinkedHashMap<String, String>();
        BufferedReader lines = new BufferedReader(reader);
        String section = "";
        String line;
        while ((line = lines.readLine()) != null) {
            line = line.trim();
            if (line.isEmpty() || line.startsWith("#")) {
                continue;                                   // blank lines and comments
            }
            if (line.startsWith("[") && line.endsWith("]")) {
                section = line.substring(1, line.length() - 1).trim();
                continue;
            }
            int eq = line.indexOf('=');
            if (eq <= 0) {
                continue;                                   // not a setting: skip it, don't throw
            }
            String key = line.substring(0, eq).trim();
            String value = line.substring(eq + 1).trim();
            values.put(section.isEmpty() ? key : section + "." + key, value);
        }
        return values;
    }

    @Override
    public void write(Map<String, String> values, Writer writer) throws IOException {
        // Group the keys by section, keeping the order in which sections and keys first appear.
        Map<String, Map<String, String>> sections = new LinkedHashMap<String, Map<String, String>>();
        for (Map.Entry<String, String> e : values.entrySet()) {
            int dot = e.getKey().indexOf('.');
            String section = dot < 0 ? "" : e.getKey().substring(0, dot);
            String key = dot < 0 ? e.getKey() : e.getKey().substring(dot + 1);
            Map<String, String> keys = sections.get(section);
            if (keys == null) {
                keys = new LinkedHashMap<String, String>();
                sections.put(section, keys);
            }
            keys.put(key, e.getValue());
        }
        // Keys without a section go first, so they aren't read back as part of one.
        Map<String, String> top = sections.remove("");
        if (top != null) {
            writeKeys(top, writer);
        }
        for (Map.Entry<String, Map<String, String>> s : sections.entrySet()) {
            writer.write("[" + s.getKey() + "]\n");
            writeKeys(s.getValue(), writer);
        }
    }

    private static void writeKeys(Map<String, String> keys, Writer writer) throws IOException {
        for (Map.Entry<String, String> e : keys.entrySet()) {
            writer.write(e.getKey() + " = " + e.getValue() + "\n");
        }
    }
}
```

## The plugin class

```java title="src/main/java/com/example/mymod/compat/MyModSettingsSaver.java"
package com.example.mymod.compat;

import dev.xichil.settingssaver.api.v1.ModSettings;
import dev.xichil.settingssaver.api.v1.SettingsSaverPlugin;
import dev.xichil.settingssaver.api.v1.SettingsSaverRegistrar;

/** Only Settings Saver loads this class. Nothing else in the mod may reference it. */
public final class MyModSettingsSaver implements SettingsSaverPlugin {
    @Override
    public void register(SettingsSaverRegistrar registrar) {
        registrar.register(ModSettings.builder("mymod", "My Mod", "2.1.0")
                .file("config/mymod.cfg", SectionedFormat.INSTANCE)
                .excludedByDefault("window.*")
                .build());
    }
}
```

## The service file

```text title="src/main/resources/META-INF/services/dev.xichil.settingssaver.api.v1.SettingsSaverPlugin"
com.example.mymod.compat.MyModSettingsSaver
```

## How it works

`read` turns the file above into this map, in this order:

| Key | Value |
| --- | --- |
| `language` | `en` |
| `video.fov` | `90` |
| `video.zoom` | `150` |
| `window.x` | `120` |
| `window.y` | `80` |

`write` does the reverse: it groups keys by the text before the first `.`, writes keys without a section
first, then one `[section]` block per group.

What makes it a valid format:

- **It round-trips.** Reading what `write` wrote gives the same map in the same order. Keys without a
  section are written first because a key written after a `[section]` header would be read back as part
  of that section.
- **It tolerates bad input.** Lines without `=` are skipped, not reported.
- **It is stateless.** `INSTANCE` has no fields, so one object serves every file and thread.
- **It doesn't close** the reader or writer.

With the keys flattened, the exclusion pattern `window.*` keeps the whole `[window]` section per
instance, and a player can exclude `video.fov` alone.

## What is lost

The format drops comments and blank lines: the profile's copy, and an instance file after a load, have
neither. If your file documents its own settings in comments, have your mod write them again when it
saves, or register the file without a format to keep it whole.

A section name that contains a `.` would not round-trip, because the first `.` separates section and key.
Adjust the separator if your section names need one.

## Related

- [Write your own file format](https://xichil.dev/docs/settings-saver/v1/guides/custom-format/index.md): the contract in detail.
- [Test your integration without Minecraft](https://xichil.dev/docs/settings-saver/v1/guides/testing/index.md#1-test-a-formats-round-trip): a
  round-trip test for this class.
