22
33import static org .assertj .core .api .Assertions .assertThat ;
44
5+ import java .io .BufferedWriter ;
6+ import java .io .File ;
57import java .io .FileInputStream ;
8+ import java .io .FileOutputStream ;
9+ import java .io .FileWriter ;
610import java .io .IOException ;
711import java .io .PrintWriter ;
12+ import java .nio .charset .StandardCharsets ;
813import java .nio .file .Files ;
914import java .nio .file .Paths ;
1015import java .nio .file .StandardOpenOption ;
1116
17+ import com .google .common .base .Charsets ;
18+ import com .google .common .io .CharSink ;
19+ import com .google .common .io .FileWriteMode ;
20+ import org .apache .commons .io .FileUtils ;
1221import org .junit .After ;
22+ import org .junit .Before ;
1323import org .junit .Test ;
1424
1525import com .baeldung .util .StreamUtils ;
@@ -18,6 +28,26 @@ public class FilesTest {
1828
1929 public static final String fileName = "src/main/resources/countries.properties" ;
2030
31+ @ Before
32+ @ After
33+ public void setup () throws Exception {
34+ PrintWriter writer = new PrintWriter (fileName );
35+ writer .print ("UK\r \n " + "US\r \n " + "Germany\r \n " );
36+ writer .close ();
37+ }
38+
39+ @ Test
40+ public void whenAppendToFileUsingGuava_thenCorrect () throws IOException {
41+ File file = new File (fileName );
42+ CharSink chs = com .google .common .io .Files .asCharSink (file , Charsets .UTF_8 , FileWriteMode .APPEND );
43+ chs .write ("Spain\r \n " );
44+
45+ assertThat (StreamUtils .getStringFromInputStream (
46+ new FileInputStream (fileName )))
47+ .isEqualTo ("UK\r \n " + "US\r \n " + "Germany\r \n " + "Spain\r \n " );
48+ }
49+
50+
2151 @ Test
2252 public void whenAppendToFileUsingFiles_thenCorrect () throws IOException {
2353 Files .write (Paths .get (fileName ), "Spain\r \n " .getBytes (), StandardOpenOption .APPEND );
@@ -27,10 +57,38 @@ public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
2757 .isEqualTo ("UK\r \n " + "US\r \n " + "Germany\r \n " + "Spain\r \n " );
2858 }
2959
30- @ After
31- public void revertFile () throws IOException {
32- PrintWriter writer = new PrintWriter (fileName );
33- writer .print ("UK\r \n " + "US\r \n " + "Germany\r \n " );
34- writer .close ();
60+ @ Test
61+ public void whenAppendToFileUsingFileUtils_thenCorrect () throws IOException {
62+ File file = new File (fileName );
63+ FileUtils .writeStringToFile (file , "Spain\r \n " , StandardCharsets .UTF_8 , true );
64+
65+ assertThat (StreamUtils .getStringFromInputStream (
66+ new FileInputStream (fileName )))
67+ .isEqualTo ("UK\r \n " + "US\r \n " + "Germany\r \n " + "Spain\r \n " );
68+ }
69+
70+ @ Test
71+ public void whenAppendToFileUsingFileOutputStream_thenCorrect () throws Exception {
72+ FileOutputStream fos = new FileOutputStream (fileName , true );
73+ fos .write ("Spain\r \n " .getBytes ());
74+ fos .close ();
75+
76+ assertThat (StreamUtils .getStringFromInputStream (
77+ new FileInputStream (fileName )))
78+ .isEqualTo ("UK\r \n " + "US\r \n " + "Germany\r \n " + "Spain\r \n " );
79+ }
80+
81+ @ Test
82+ public void whenAppendToFileUsingFileWriter_thenCorrect () throws IOException {
83+ FileWriter fw = new FileWriter (fileName , true );
84+ BufferedWriter bw = new BufferedWriter (fw );
85+ bw .write ("Spain" );
86+ bw .newLine ();
87+ bw .close ();
88+
89+ assertThat (
90+ StreamUtils .getStringFromInputStream (
91+ new FileInputStream (fileName )))
92+ .isEqualTo ("UK\r \n " + "US\r \n " + "Germany\r \n " + "Spain\n " );
3593 }
3694}
0 commit comments