From 5a23a6908198a709bd24edf987b8e2bd6c0cbc77 Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Fri, 27 Oct 2017 10:17:01 +0200 Subject: [PATCH 1/3] merge with fork --- .../test/java/com/baeldung/string/StringTest.java | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/string/StringTest.java b/core-java/src/test/java/com/baeldung/string/StringTest.java index 2f8aec9887..e88b2d7c2c 100644 --- a/core-java/src/test/java/com/baeldung/string/StringTest.java +++ b/core-java/src/test/java/com/baeldung/string/StringTest.java @@ -5,10 +5,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -<<<<<<< HEAD -======= import java.io.UnsupportedEncodingException; ->>>>>>> ef4ee45a18de65b0c81bbe8da16c0b063b2201a5 import java.nio.charset.StandardCharsets; import java.util.IllegalFormatException; import java.util.regex.PatternSyntaxException; @@ -33,13 +30,6 @@ public class StringTest { } @Test -<<<<<<< HEAD - public void whenGetBytes_thenCorrect() { - byte[] byteArray = "abcd".getBytes(); - byte[] expected = new byte[] { 97, 98, 99, 100 }; - - assertArrayEquals(expected, byteArray); -======= public void whenGetBytes_thenCorrect() throws UnsupportedEncodingException { byte[] byteArray1 = "abcd".getBytes(); byte[] byteArray2 = "efgh".getBytes(StandardCharsets.US_ASCII); @@ -51,7 +41,6 @@ public class StringTest { assertArrayEquals(expected1, byteArray1); assertArrayEquals(expected2, byteArray2); assertArrayEquals(expected3, byteArray3); ->>>>>>> ef4ee45a18de65b0c81bbe8da16c0b063b2201a5 } @Test @@ -234,4 +223,4 @@ public class StringTest { assertEquals("200", String.valueOf(l)); } -} +} \ No newline at end of file From 3cc88518bd0ac009d8ad568b708a571bf938d04b Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Wed, 15 Nov 2017 23:40:35 +0200 Subject: [PATCH 2/3] fix compilation error --- .../java/com/baeldung/concurrent/daemon/DaemonThreadTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/daemon/DaemonThreadTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/daemon/DaemonThreadTest.java index f0150578ae..3ca69d8847 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/daemon/DaemonThreadTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/daemon/DaemonThreadTest.java @@ -3,10 +3,7 @@ package com.baeldung.concurrent.daemon; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -<<<<<<< HEAD -======= import org.junit.Ignore; ->>>>>>> d54917c7e9f0f74c40982571af8ac9f61782b7cb import org.junit.Test; public class DaemonThreadTest { From 960d8e004c4babafcb3e9762e294a839ca6b189e Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Thu, 16 Nov 2017 06:44:34 +0200 Subject: [PATCH 3/3] BAEL-1309 Append Data to a File with Java --- .../baeldung/file/FileOutputStreamTest.java | 37 ++++++++++++++++ .../java/com/baeldung/file/FileUtilsTest.java | 38 +++++++++++++++++ .../com/baeldung/file/FileWriterTest.java | 42 +++++++++++++++++++ .../java/com/baeldung/file/FilesTest.java | 37 ++++++++++++++++ .../java/com/baeldung/file/GuavaTest.java | 41 ++++++++++++++++++ .../java/com/baeldung/util/StreamUtils.java | 16 +++++++ 6 files changed, 211 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/file/FileOutputStreamTest.java create mode 100644 core-java/src/main/java/com/baeldung/file/FileUtilsTest.java create mode 100644 core-java/src/main/java/com/baeldung/file/FileWriterTest.java create mode 100644 core-java/src/main/java/com/baeldung/file/FilesTest.java create mode 100644 core-java/src/main/java/com/baeldung/file/GuavaTest.java create mode 100644 core-java/src/main/java/com/baeldung/util/StreamUtils.java diff --git a/core-java/src/main/java/com/baeldung/file/FileOutputStreamTest.java b/core-java/src/main/java/com/baeldung/file/FileOutputStreamTest.java new file mode 100644 index 0000000000..d85c028dae --- /dev/null +++ b/core-java/src/main/java/com/baeldung/file/FileOutputStreamTest.java @@ -0,0 +1,37 @@ +package com.baeldung.file; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintWriter; + +import org.junit.After; +import org.junit.Test; + +import com.baeldung.util.StreamUtils; + +public class FileOutputStreamTest { + + public static final String fileName = "src/main/resources/countries.txt"; + + @Test + public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception { + FileOutputStream fos = new FileOutputStream(fileName, true); + fos.write("Spain\r\n".getBytes()); + fos.close(); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @After + public void revertFile() throws IOException { + PrintWriter writer = new PrintWriter(fileName); + writer.print(""); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } +} diff --git a/core-java/src/main/java/com/baeldung/file/FileUtilsTest.java b/core-java/src/main/java/com/baeldung/file/FileUtilsTest.java new file mode 100644 index 0000000000..6147653976 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/file/FileUtilsTest.java @@ -0,0 +1,38 @@ +package com.baeldung.file; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Test; + +import com.baeldung.util.StreamUtils; + +public class FileUtilsTest { + + public static final String fileName = "src/main/resources/countries.txt"; + + @Test + public void whenAppendToFileUsingFiles_thenCorrect() throws IOException { + File file = new File(fileName); + FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @After + public void revertFile() throws IOException { + PrintWriter writer = new PrintWriter(fileName); + writer.print(""); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } +} diff --git a/core-java/src/main/java/com/baeldung/file/FileWriterTest.java b/core-java/src/main/java/com/baeldung/file/FileWriterTest.java new file mode 100644 index 0000000000..67e4dfaf20 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/file/FileWriterTest.java @@ -0,0 +1,42 @@ +package com.baeldung.file; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.BufferedWriter; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; + +import org.junit.After; +import org.junit.Test; + +import com.baeldung.util.StreamUtils; + +public class FileWriterTest { + + public static final String fileName = "src/main/resources/countries.txt"; + + @Test + public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException { + String fileName = "src/main/resources/countries.txt"; + FileWriter fw = new FileWriter(fileName, true); + BufferedWriter bw = new BufferedWriter(fw); + bw.write("Spain"); + bw.newLine(); + bw.close(); + + assertThat( + StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @After + public void revertFile() throws IOException { + PrintWriter writer = new PrintWriter(fileName); + writer.print(""); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } +} diff --git a/core-java/src/main/java/com/baeldung/file/FilesTest.java b/core-java/src/main/java/com/baeldung/file/FilesTest.java new file mode 100644 index 0000000000..0ca69db8ab --- /dev/null +++ b/core-java/src/main/java/com/baeldung/file/FilesTest.java @@ -0,0 +1,37 @@ +package com.baeldung.file; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; + +import org.junit.After; +import org.junit.Test; + +import com.baeldung.util.StreamUtils; + +public class FilesTest { + + public static final String fileName = "src/main/resources/countries.txt"; + + @Test + public void whenAppendToFileUsingFiles_thenCorrect() throws IOException { + Files.write(Paths.get(fileName), "Spain\r\n".getBytes(), StandardOpenOption.APPEND); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @After + public void revertFile() throws IOException { + PrintWriter writer = new PrintWriter(fileName); + writer.print(""); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } +} diff --git a/core-java/src/main/java/com/baeldung/file/GuavaTest.java b/core-java/src/main/java/com/baeldung/file/GuavaTest.java new file mode 100644 index 0000000000..06379451f9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/file/GuavaTest.java @@ -0,0 +1,41 @@ +package com.baeldung.file; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.PrintWriter; + +import org.junit.After; +import org.junit.Test; + +import com.baeldung.util.StreamUtils; +import com.google.common.base.Charsets; +import com.google.common.io.CharSink; +import com.google.common.io.FileWriteMode; +import com.google.common.io.Files; + +public class GuavaTest { + + public static final String fileName = "src/main/resources/countries.txt"; + + @Test + public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException { + File file = new File(fileName); + CharSink chs = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND); + chs.write("Spain\r\n"); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @After + public void revertFile() throws IOException { + PrintWriter writer = new PrintWriter(fileName); + writer.print(""); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } +} diff --git a/core-java/src/main/java/com/baeldung/util/StreamUtils.java b/core-java/src/main/java/com/baeldung/util/StreamUtils.java new file mode 100644 index 0000000000..42f438732f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/util/StreamUtils.java @@ -0,0 +1,16 @@ +package com.baeldung.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; + +import org.apache.commons.io.IOUtils; + +public class StreamUtils { + + public static String getStringFromInputStream(InputStream input) throws IOException { + StringWriter writer = new StringWriter(); + IOUtils.copy(input, writer, "UTF-8"); + return writer.toString(); + } +}