Skip to content

Commit cf9193b

Browse files
committed
feat: Writing to file implemented
1 parent 23a36c9 commit cf9193b

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/main/java/org/codejive/properties/Properties.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public String remove(Object key) {
6666
}
6767

6868
public void load(Path file) throws IOException {
69-
load(Files.newBufferedReader(file));
69+
try (Reader br = Files.newBufferedReader(file)) {
70+
load(br);
71+
}
7072
}
7173

7274
public void load(Reader reader) throws IOException {
@@ -101,8 +103,14 @@ public static Properties loadProperties(Reader reader) throws IOException {
101103
}
102104

103105
public void store(Path file) throws IOException {
104-
store(Files.newBufferedWriter(file, StandardOpenOption.TRUNCATE_EXISTING));
106+
try (Writer bw = Files.newBufferedWriter(file, StandardOpenOption.TRUNCATE_EXISTING)) {
107+
store(bw);
108+
}
105109
}
106110

107-
public void store(Writer writer) throws IOException {}
111+
public void store(Writer writer) throws IOException {
112+
for (PropertiesParser.Token token : tokens) {
113+
writer.write(token.getRaw());
114+
}
115+
}
108116
}

src/test/java/org/codejive/properties/TestProperties.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.junit.jupiter.api.Test;
44

55
import java.io.IOException;
6+
import java.io.StringWriter;
67
import java.net.URISyntaxException;
8+
import java.nio.file.Files;
79
import java.nio.file.Path;
810
import java.nio.file.Paths;
911

@@ -23,4 +25,13 @@ void testLoad() throws IOException, URISyntaxException {
2325
assertThat(p.values(), contains("simple", "value containing spaces", "and escapes\n\t\r\f", "everywhere",
2426
"value", "one \n two \n\tthree", "\u1234"));
2527
}
28+
29+
@Test
30+
void testStore() throws IOException, URISyntaxException {
31+
Path f = Paths.get(getClass().getResource("/test.properties").toURI());
32+
Properties p = Properties.loadProperties(f);
33+
StringWriter sw = new StringWriter();
34+
p.store(sw);
35+
assertThat(sw.toString(), equalTo(new String(Files.readAllBytes(f))));
36+
}
2637
}

0 commit comments

Comments
 (0)