Skip to content

Commit 841c4cc

Browse files
committed
write & append to file.
1 parent 76bce5e commit 841c4cc

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

src/main/java/fr/formiko/utils/FLUFiles.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.net.URL;
77
import java.nio.file.Files;
88
import java.nio.file.Paths;
9+
import java.nio.file.StandardOpenOption;
910
import java.util.List;
1011

1112
/**
@@ -33,8 +34,8 @@ private FLUFiles() {} // hide constructor
3334
public static String readFile(String path) { return internal.readFile(path); }
3435
public static List<String> readFileAsList(String path) { return internal.readFileAsList(path); }
3536
public static String readFileFromWeb(String url) { return internal.readFileFromWeb(url); }
36-
public static boolean writeFile(String path, String content) { return false; }
37-
public static boolean appendToFile(String path, String content) { return false; }
37+
public static boolean writeFile(String path, String content) { return internal.writeFile(path, content); }
38+
public static boolean appendToFile(String path, String content) { return internal.appendToFile(path, content); }
3839

3940
public static List<String> listFiles(String path) { return null; }
4041

@@ -171,5 +172,39 @@ private String readFileFromWeb(String urlString) {
171172
return null;
172173
}
173174
}
175+
176+
/**
177+
* It will create the file and it's path if needed.
178+
*/
179+
private boolean writeFile(String path, String content) {
180+
if (isAValidePath(path)) {
181+
new File(path).getParentFile().mkdirs();
182+
try {
183+
Files.writeString(Paths.get(path), content, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
184+
return true;
185+
} catch (IOException e) {
186+
return false;
187+
}
188+
} else {
189+
return false;
190+
}
191+
}
192+
193+
/**
194+
* It will create the file and it's path if needed.
195+
*/
196+
private boolean appendToFile(String path, String content) {
197+
if (isAValidePath(path)) {
198+
new File(path).getParentFile().mkdirs();
199+
try {
200+
Files.writeString(Paths.get(path), content, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
201+
return true;
202+
} catch (IOException e) {
203+
return false;
204+
}
205+
} else {
206+
return false;
207+
}
208+
}
174209
}
175210
}

src/test/java/fr/formiko/utils/FLUFilesTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,56 @@ private static Stream<Arguments> testReadFileFromWebSource() { return Stream.of(
201201
"message": "100%",
202202
"color": "00FF00"
203203
}"""), Arguments.of(null, false, null), Arguments.of("h://unexisting.url", false, null)); }
204+
205+
@ParameterizedTest
206+
@MethodSource("testWriteFileSource")
207+
void testWriteFile(String path, boolean shouldWork, String content) {
208+
assertEquals(shouldWork, FLUFiles.writeFile(path, content));
209+
if (shouldWork) {
210+
assertEquals(content, FLUFiles.readFile(path));
211+
assertEquals(true, FLUFiles.delete(path));
212+
}
213+
}
214+
215+
private static Stream<Arguments> testWriteFileSource() {
216+
return Stream.of(Arguments.of(TEST_PATH_TEMPORARY + "testWriteFile1.txt", true, "Some content."),
217+
Arguments.of(TEST_PATH_TEMPORARY + "testWriteFile2.txt", true, "Some content."),
218+
Arguments.of(TEST_PATH_TEMPORARY + "éà@--", true, "Some content."), Arguments.of(null, false, "Some vyzemjzefze"),
219+
Arguments.of(TEST_PATH_TEMPORARY + "DIR/2/out.in", true, "Some content"),
220+
Arguments.of(TEST_PATH_TEMPORARY + "existingFile2", true, "Some content"));
221+
}
222+
223+
@ParameterizedTest
224+
@MethodSource("testAppendToFileSource")
225+
void testAppendToFile(String path, boolean shouldWork, String contentToWrite, String expectedContent) {
226+
assertEquals(shouldWork, FLUFiles.appendToFile(path, contentToWrite));
227+
if (shouldWork) {
228+
assertEquals(expectedContent, FLUFiles.readFile(path));
229+
assertEquals(true, FLUFiles.delete(path));
230+
}
231+
}
232+
233+
private static Stream<Arguments> testAppendToFileSource() {
234+
return Stream.of(Arguments.of(TEST_PATH_TEMPORARY + "testAppendToFile1.txt", true, "Some content.", "Some content."),
235+
Arguments.of(TEST_PATH_TEMPORARY + "testAppendToFile2.txt", true, "Some content.", "Some content."),
236+
Arguments.of(TEST_PATH_TEMPORARY + "éà@--", true, "Some content.", "Some content."),
237+
Arguments.of(null, false, "Some vyzemjzefze", ""));
238+
}
239+
240+
@ParameterizedTest
241+
@MethodSource("testAppendToExistingFileFileSource")
242+
void testAppendToExistingFile(String path, boolean shouldWork, String contentToWrite, String expectedContent, String copyPath) {
243+
assertEquals(true, FLUFiles.copy(path, copyPath));
244+
assertEquals(shouldWork, FLUFiles.appendToFile(copyPath, contentToWrite));
245+
if (shouldWork) {
246+
assertEquals(expectedContent, FLUFiles.readFile(copyPath));
247+
}
248+
}
249+
250+
private static Stream<Arguments> testAppendToExistingFileFileSource() {
251+
return Stream.of(
252+
Arguments.of(TEST_PATH + "existingFile3", true, "Some content", "ABCSome content", TEST_PATH_TEMPORARY + "existingFile3"),
253+
Arguments.of(TEST_PATH + "existingFile4", true, "Some content", "ABC\nSome content",
254+
TEST_PATH_TEMPORARY + "existingFile4"));
255+
}
204256
}

src/test/resources/existingFile2

Whitespace-only changes.

src/test/resources/existingFile3

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ABC

src/test/resources/existingFile4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ABC

0 commit comments

Comments
 (0)