Skip to content

Commit 960d8e0

Browse files
author
Ahmed Tawila
committed
BAEL-1309 Append Data to a File with Java
1 parent 3cc8851 commit 960d8e0

6 files changed

Lines changed: 211 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.file;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.io.PrintWriter;
9+
10+
import org.junit.After;
11+
import org.junit.Test;
12+
13+
import com.baeldung.util.StreamUtils;
14+
15+
public class FileOutputStreamTest {
16+
17+
public static final String fileName = "src/main/resources/countries.txt";
18+
19+
@Test
20+
public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception {
21+
FileOutputStream fos = new FileOutputStream(fileName, true);
22+
fos.write("Spain\r\n".getBytes());
23+
fos.close();
24+
25+
assertThat(StreamUtils.getStringFromInputStream(
26+
new FileInputStream(fileName)))
27+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
28+
}
29+
30+
@After
31+
public void revertFile() throws IOException {
32+
PrintWriter writer = new PrintWriter(fileName);
33+
writer.print("");
34+
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
35+
writer.close();
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.file;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.IOException;
8+
import java.io.PrintWriter;
9+
import java.nio.charset.StandardCharsets;
10+
11+
import org.apache.commons.io.FileUtils;
12+
import org.junit.After;
13+
import org.junit.Test;
14+
15+
import com.baeldung.util.StreamUtils;
16+
17+
public class FileUtilsTest {
18+
19+
public static final String fileName = "src/main/resources/countries.txt";
20+
21+
@Test
22+
public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
23+
File file = new File(fileName);
24+
FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true);
25+
26+
assertThat(StreamUtils.getStringFromInputStream(
27+
new FileInputStream(fileName)))
28+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
29+
}
30+
31+
@After
32+
public void revertFile() throws IOException {
33+
PrintWriter writer = new PrintWriter(fileName);
34+
writer.print("");
35+
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
36+
writer.close();
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.file;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.BufferedWriter;
6+
import java.io.FileInputStream;
7+
import java.io.FileWriter;
8+
import java.io.IOException;
9+
import java.io.PrintWriter;
10+
11+
import org.junit.After;
12+
import org.junit.Test;
13+
14+
import com.baeldung.util.StreamUtils;
15+
16+
public class FileWriterTest {
17+
18+
public static final String fileName = "src/main/resources/countries.txt";
19+
20+
@Test
21+
public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
22+
String fileName = "src/main/resources/countries.txt";
23+
FileWriter fw = new FileWriter(fileName, true);
24+
BufferedWriter bw = new BufferedWriter(fw);
25+
bw.write("Spain");
26+
bw.newLine();
27+
bw.close();
28+
29+
assertThat(
30+
StreamUtils.getStringFromInputStream(
31+
new FileInputStream(fileName)))
32+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
33+
}
34+
35+
@After
36+
public void revertFile() throws IOException {
37+
PrintWriter writer = new PrintWriter(fileName);
38+
writer.print("");
39+
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
40+
writer.close();
41+
}
42+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.file;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
import java.io.PrintWriter;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import java.nio.file.StandardOpenOption;
11+
12+
import org.junit.After;
13+
import org.junit.Test;
14+
15+
import com.baeldung.util.StreamUtils;
16+
17+
public class FilesTest {
18+
19+
public static final String fileName = "src/main/resources/countries.txt";
20+
21+
@Test
22+
public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
23+
Files.write(Paths.get(fileName), "Spain\r\n".getBytes(), StandardOpenOption.APPEND);
24+
25+
assertThat(StreamUtils.getStringFromInputStream(
26+
new FileInputStream(fileName)))
27+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
28+
}
29+
30+
@After
31+
public void revertFile() throws IOException {
32+
PrintWriter writer = new PrintWriter(fileName);
33+
writer.print("");
34+
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
35+
writer.close();
36+
}
37+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.file;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.IOException;
8+
import java.io.PrintWriter;
9+
10+
import org.junit.After;
11+
import org.junit.Test;
12+
13+
import com.baeldung.util.StreamUtils;
14+
import com.google.common.base.Charsets;
15+
import com.google.common.io.CharSink;
16+
import com.google.common.io.FileWriteMode;
17+
import com.google.common.io.Files;
18+
19+
public class GuavaTest {
20+
21+
public static final String fileName = "src/main/resources/countries.txt";
22+
23+
@Test
24+
public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
25+
File file = new File(fileName);
26+
CharSink chs = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
27+
chs.write("Spain\r\n");
28+
29+
assertThat(StreamUtils.getStringFromInputStream(
30+
new FileInputStream(fileName)))
31+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
32+
}
33+
34+
@After
35+
public void revertFile() throws IOException {
36+
PrintWriter writer = new PrintWriter(fileName);
37+
writer.print("");
38+
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
39+
writer.close();
40+
}
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.util;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.StringWriter;
6+
7+
import org.apache.commons.io.IOUtils;
8+
9+
public class StreamUtils {
10+
11+
public static String getStringFromInputStream(InputStream input) throws IOException {
12+
StringWriter writer = new StringWriter();
13+
IOUtils.copy(input, writer, "UTF-8");
14+
return writer.toString();
15+
}
16+
}

0 commit comments

Comments
 (0)