All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.deplant.java4ever.binding.io.JsonFile Maven / Gradle / Ivy

package tech.deplant.java4ever.binding.io;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * Helper class for working with JSON-organized files.
 */
public record JsonFile(String filePath) implements Supplier, Consumer {
	@Override
	public String get() {
		try {
			return Files.readString(Paths.get(filePath()).toAbsolutePath())
			            .replaceAll("[\u0000-\u001f]", "");
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}


	@Override
	public void accept(String jsonString) {
		try {
			var path = Paths.get(filePath());
			if (!Files.exists(path)) {
				Files.createDirectories(path.getParent());
				Files.createFile(path);
			}
			Files.writeString(path,
			                  jsonString);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy