mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 13:38:23 +02:00
extracted load and save service and generalize file writer
This commit is contained in:
@@ -29,23 +29,29 @@ package ninja.javafx.smartcsv.preferences;
|
||||
import com.typesafe.config.Config;
|
||||
import com.typesafe.config.ConfigFactory;
|
||||
import com.typesafe.config.ConfigRenderOptions;
|
||||
import ninja.javafx.smartcsv.FileWriter;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.supercsv.prefs.CsvPreference;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.nio.file.Files.write;
|
||||
|
||||
/**
|
||||
* Save preferences to configuration file
|
||||
*/
|
||||
@Service
|
||||
public class PreferencesFileWriter {
|
||||
public class PreferencesFileWriter implements FileWriter {
|
||||
|
||||
public void saveFile(File file, CsvPreference csvPreference) throws IOException {
|
||||
private CsvPreference csvPreference;
|
||||
|
||||
public void setCsvPreference(CsvPreference csvPreference) {
|
||||
this.csvPreference = csvPreference;
|
||||
}
|
||||
|
||||
public void write(File file) throws IOException {
|
||||
Map<String, Object> preferences = new HashMap<>();
|
||||
preferences.put("quoteChar", Character.toString(csvPreference.getQuoteChar()));
|
||||
preferences.put("delimiterChar", Character.toString((char)csvPreference.getDelimiterChar()));
|
||||
@@ -55,7 +61,7 @@ public class PreferencesFileWriter {
|
||||
preferences.put("quoteMode", QuoteModeHelper.getQuoteModeName(csvPreference.getQuoteMode()));
|
||||
|
||||
Config config = ConfigFactory.parseMap(preferences);
|
||||
write(file.toPath(), config.root().render(ConfigRenderOptions.concise()).getBytes());
|
||||
Files.write(file.toPath(), config.root().render(ConfigRenderOptions.concise()).getBytes());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user