File tree Expand file tree Collapse file tree
test/java/com/baeldung/file Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ UK
2+ US
3+ Germany
Original file line number Diff line number Diff line change 1414
1515public class FileOutputStreamTest {
1616
17- public static final String fileName = "src/main/resources/countries.txt " ;
17+ public static final String fileName = "src/main/resources/countries.properties " ;
1818
1919 @ Test
2020 public void whenAppendToFileUsingFileOutputStream_thenCorrect () throws Exception {
@@ -30,7 +30,6 @@ public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception
3030 @ After
3131 public void revertFile () throws IOException {
3232 PrintWriter writer = new PrintWriter (fileName );
33- writer .print ("" );
3433 writer .print ("UK\r \n " + "US\r \n " + "Germany\r \n " );
3534 writer .close ();
3635 }
Original file line number Diff line number Diff line change 1616
1717public class FileUtilsTest {
1818
19- public static final String fileName = "src/main/resources/countries.txt " ;
19+ public static final String fileName = "src/main/resources/countries.properties " ;
2020
2121 @ Test
2222 public void whenAppendToFileUsingFiles_thenCorrect () throws IOException {
@@ -31,7 +31,6 @@ public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
3131 @ After
3232 public void revertFile () throws IOException {
3333 PrintWriter writer = new PrintWriter (fileName );
34- writer .print ("" );
3534 writer .print ("UK\r \n " + "US\r \n " + "Germany\r \n " );
3635 writer .close ();
3736 }
Original file line number Diff line number Diff line change 1515
1616public class FileWriterTest {
1717
18- public static final String fileName = "src/main/resources/countries.txt " ;
18+ public static final String fileName = "src/main/resources/countries.properties " ;
1919
2020 @ Test
2121 public void whenAppendToFileUsingFileWriter_thenCorrect () throws IOException {
22- String fileName = "src/main/resources/countries.txt" ;
2322 FileWriter fw = new FileWriter (fileName , true );
2423 BufferedWriter bw = new BufferedWriter (fw );
2524 bw .write ("Spain" );
@@ -29,13 +28,12 @@ public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
2928 assertThat (
3029 StreamUtils .getStringFromInputStream (
3130 new FileInputStream (fileName )))
32- .isEqualTo ("UK\r \n " + "US\r \n " + "Germany\r \n " + "Spain\r \ n " );
31+ .isEqualTo ("UK\r \n " + "US\r \n " + "Germany\r \n " + "Spain\n " );
3332 }
3433
3534 @ After
3635 public void revertFile () throws IOException {
3736 PrintWriter writer = new PrintWriter (fileName );
38- writer .print ("" );
3937 writer .print ("UK\r \n " + "US\r \n " + "Germany\r \n " );
4038 writer .close ();
4139 }
Original file line number Diff line number Diff line change 1616
1717public class FilesTest {
1818
19- public static final String fileName = "src/main/resources/countries.txt " ;
19+ public static final String fileName = "src/main/resources/countries.properties " ;
2020
2121 @ Test
2222 public void whenAppendToFileUsingFiles_thenCorrect () throws IOException {
@@ -30,7 +30,6 @@ public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
3030 @ After
3131 public void revertFile () throws IOException {
3232 PrintWriter writer = new PrintWriter (fileName );
33- writer .print ("" );
3433 writer .print ("UK\r \n " + "US\r \n " + "Germany\r \n " );
3534 writer .close ();
3635 }
Original file line number Diff line number Diff line change 88import java .io .PrintWriter ;
99
1010import org .junit .After ;
11+ import org .junit .Before ;
1112import org .junit .Test ;
1213
1314import com .baeldung .util .StreamUtils ;
1819
1920public class GuavaTest {
2021
21- public static final String fileName = "src/main/resources/countries.txt" ;
22+ public static final String fileName = "src/main/resources/countries.properties" ;
23+
24+ @ Before
25+ public void setup () throws Exception {
26+ PrintWriter writer = new PrintWriter (fileName );
27+ writer .print ("UK\r \n " + "US\r \n " + "Germany\r \n " );
28+ writer .close ();
29+ }
2230
2331 @ Test
24- public void whenAppendToFileUsingFileWriter_thenCorrect () throws IOException {
32+ public void whenAppendToFileUsingGuava_thenCorrect () throws IOException {
2533 File file = new File (fileName );
2634 CharSink chs = Files .asCharSink (file , Charsets .UTF_8 , FileWriteMode .APPEND );
2735 chs .write ("Spain\r \n " );
@@ -34,7 +42,6 @@ public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
3442 @ After
3543 public void revertFile () throws IOException {
3644 PrintWriter writer = new PrintWriter (fileName );
37- writer .print ("" );
3845 writer .print ("UK\r \n " + "US\r \n " + "Germany\r \n " );
3946 writer .close ();
4047 }
You can’t perform that action at this time.
0 commit comments