11package com .baeldung .filewriter ;
22
3+ import java .io .FileWriter ;
34import java .io .IOException ;
45import java .nio .file .Files ;
56import java .nio .file .Path ;
6- import java .util .ArrayList ;
7- import java .util .List ;
8- import java .util .Random ;
9- import java .util .stream .IntStream ;
107
118import org .junit .After ;
129import org .junit .Assert ;
1512public class FileWriterExampleUnitTest {
1613
1714 private static final String FILE_NAME = "src/test/resources/FileWriterTest.txt" ;
18- private static final String STRING_TO_WRITE = "Hello Folks!" ;
19- private static final String STRING_TO_APPEND = "Hello Folks Again!" ;
20- private static final char [] CHAR_ARRAY_TO_WRITE = STRING_TO_WRITE .toCharArray ();
21-
22- private FileWriterExample fileWriterExample = new FileWriterExample ();
2315
2416 @ After
2517 public void tearDown () throws IOException {
@@ -28,30 +20,30 @@ public void tearDown() throws IOException {
2820
2921 @ Test
3022 public void testWriteString () throws IOException {
31- fileWriterExample .writeString (FILE_NAME , STRING_TO_WRITE );
32- Assert .assertEquals (STRING_TO_WRITE , new String (Files .readAllBytes (Path .of (FILE_NAME ))));
23+ try (FileWriter fileWriter = new FileWriter (FILE_NAME )) {
24+ fileWriter .write ("Hello Folks!" );
25+ }
26+ Assert .assertEquals ("Hello Folks!" , new String (Files .readAllBytes (Path .of (FILE_NAME ))));
3327 }
3428
3529 @ Test
3630 public void testAppendString () throws IOException {
37- fileWriterExample .writeString (FILE_NAME , STRING_TO_WRITE );
38- fileWriterExample .appendString (FILE_NAME , STRING_TO_APPEND );
39- Assert .assertEquals (STRING_TO_WRITE + STRING_TO_APPEND , new String (Files .readAllBytes (Path .of (FILE_NAME ))));
31+ try (FileWriter fileWriter = new FileWriter (FILE_NAME )) {
32+ fileWriter .write ("Hello Folks!" );
33+ }
34+ // using another try with resources to reopen the file in append mode
35+ try (FileWriter fileWriter = new FileWriter (FILE_NAME , true )) {
36+ fileWriter .write ("Hello Folks Again!" );
37+ }
38+
39+ Assert .assertEquals ("Hello Folks!" + "Hello Folks Again!" , new String (Files .readAllBytes (Path .of (FILE_NAME ))));
4040 }
4141
4242 @ Test
4343 public void testWriteCharArray () throws IOException {
44- fileWriterExample .writeCharArray (FILE_NAME , CHAR_ARRAY_TO_WRITE );
45- Assert .assertEquals (STRING_TO_WRITE , new String (Files .readAllBytes (Path .of (FILE_NAME ))));
46- }
47-
48- @ Test
49- public void testWriteWithBufferedWriter () throws IOException {
50- final List <String > stringsToWrite = new ArrayList <>();
51- for (int i =0 ; i < 10000 ;i ++) {
52- stringsToWrite .add ("Random string " +i );
44+ try (FileWriter fileWriter = new FileWriter (FILE_NAME )) {
45+ fileWriter .write ("Hello Folks!" .toCharArray ());
5346 }
54- fileWriterExample .writeWithBufferedWriter (FILE_NAME , stringsToWrite );
55- Assert .assertNotNull (new String (Files .readAllBytes (Path .of (FILE_NAME ))));
47+ Assert .assertEquals ("Hello Folks!" , new String (Files .readAllBytes (Path .of (FILE_NAME ))));
5648 }
5749}
0 commit comments