diff --git a/core-java-modules/core-java-11-3/src/main/java/com/baeldung/filewriter/BufferedWriterExample.java b/core-java-modules/core-java-11-3/src/main/java/com/baeldung/filewriter/BufferedWriterExample.java new file mode 100644 index 0000000000..705f38d4ce --- /dev/null +++ b/core-java-modules/core-java-11-3/src/main/java/com/baeldung/filewriter/BufferedWriterExample.java @@ -0,0 +1,19 @@ +package com.baeldung.filewriter; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; + +public class BufferedWriterExample { + + public static String generateFileFromStringList(List stringList) throws IOException { + final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; + BufferedWriter br = new BufferedWriter(new FileWriter(TEXT_FILENAME)); + for (String str : stringList) { + br.write(str + System.lineSeparator()); + } + br.close(); + return TEXT_FILENAME; + } +} diff --git a/core-java-modules/core-java-11-3/src/main/java/com/baeldung/filewriter/FileWriteStringExample.java b/core-java-modules/core-java-11-3/src/main/java/com/baeldung/filewriter/FileWriteStringExample.java new file mode 100644 index 0000000000..3c72a53001 --- /dev/null +++ b/core-java-modules/core-java-11-3/src/main/java/com/baeldung/filewriter/FileWriteStringExample.java @@ -0,0 +1,22 @@ +package com.baeldung.filewriter; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.List; + +public class FileWriteStringExample { + public static String generateFileFromStringList(List stringList) throws IOException { + final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; + Path filePath = Paths.get(TEXT_FILENAME); + Files.deleteIfExists(filePath); + Files.createFile(filePath); + for (String str : stringList) { + Files.writeString(filePath, str + System.lineSeparator(), + StandardOpenOption.APPEND); + } + return filePath.toString(); + } +} diff --git a/core-java-modules/core-java-11-3/src/main/java/com/baeldung/filewriter/FileWriterExample.java b/core-java-modules/core-java-11-3/src/main/java/com/baeldung/filewriter/FileWriterExample.java new file mode 100644 index 0000000000..fcc7305d59 --- /dev/null +++ b/core-java-modules/core-java-11-3/src/main/java/com/baeldung/filewriter/FileWriterExample.java @@ -0,0 +1,18 @@ +package com.baeldung.filewriter; + +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; + +public class FileWriterExample { + + public static String generateFileFromStringList(List stringList) throws IOException { + final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; + FileWriter fileWriter = new FileWriter(TEXT_FILENAME); + for (String str : stringList) { + fileWriter.write(str + System.lineSeparator()); + } + fileWriter.close(); + return TEXT_FILENAME; + } +} diff --git a/core-java-modules/core-java-11-3/src/test/java/com/baeldung/filewriter/BufferedWriterUnitTest.java b/core-java-modules/core-java-11-3/src/test/java/com/baeldung/filewriter/BufferedWriterUnitTest.java new file mode 100644 index 0000000000..8c45ac07b3 --- /dev/null +++ b/core-java-modules/core-java-11-3/src/test/java/com/baeldung/filewriter/BufferedWriterUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.filewriter; + +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; + +public class BufferedWriterUnitTest { + + private static final List stringList = Arrays.asList("Hello", "World"); + + @Test + public void givenUsingBufferedWriter_whenStringList_thenGetTextFile() throws IOException { + String fileName = BufferedWriterExample.generateFileFromStringList(stringList); + long count = Files.lines(Paths.get(fileName)).count(); + assertTrue("No. of lines in file should be equal to no. of Strings in List", ((int) count) == stringList.size()); + } +} diff --git a/core-java-modules/core-java-11-3/src/test/java/com/baeldung/filewriter/FileWriteStringUnitTest.java b/core-java-modules/core-java-11-3/src/test/java/com/baeldung/filewriter/FileWriteStringUnitTest.java new file mode 100644 index 0000000000..fbae98e701 --- /dev/null +++ b/core-java-modules/core-java-11-3/src/test/java/com/baeldung/filewriter/FileWriteStringUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.filewriter; + +import org.junit.jupiter.api.Test; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertTrue; + + +public class FileWriteStringUnitTest { + + private static final List stringList = Arrays.asList("Hello", "World"); + + @Test + public void givenUsingFileWriteString_whenStringList_thenGetTextFile() throws IOException { + String fileName = FileWriteStringExample.generateFileFromStringList(stringList); + long count = Files.lines(Paths.get(fileName)).count(); + assertTrue("No. of lines in file should be equal to no. of Strings in List", ((int) count) == stringList.size()); + } +} diff --git a/core-java-modules/core-java-11-3/src/test/java/com/baeldung/filewriter/FileWriterUnitTest.java b/core-java-modules/core-java-11-3/src/test/java/com/baeldung/filewriter/FileWriterUnitTest.java new file mode 100644 index 0000000000..f5dd3d1a70 --- /dev/null +++ b/core-java-modules/core-java-11-3/src/test/java/com/baeldung/filewriter/FileWriterUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.filewriter; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertTrue; + +public class FileWriterUnitTest { + + private static final List stringList = Arrays.asList("Hello", "World"); + + @Test + public void givenUsingFileWriter_whenStringList_thenGetTextFile() throws IOException { + String fileName = FileWriterExample.generateFileFromStringList(stringList); + long count = Files.lines(Paths.get(fileName)).count(); + assertTrue("No. of lines in file should be equal to no. of Strings in List", ((int) count) == stringList.size()); + } +} diff --git a/core-java-modules/core-java-11-3/src/test/resources/sampleTextFile.txt b/core-java-modules/core-java-11-3/src/test/resources/sampleTextFile.txt new file mode 100644 index 0000000000..f9264f7fbd --- /dev/null +++ b/core-java-modules/core-java-11-3/src/test/resources/sampleTextFile.txt @@ -0,0 +1,2 @@ +Hello +World diff --git a/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/filewriter/FileWriterExampleUnitTest.java b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/FileWriterExampleUnitTest.java similarity index 98% rename from core-java-modules/core-java-io-apis/src/test/java/com/baeldung/filewriter/FileWriterExampleUnitTest.java rename to core-java-modules/core-java-io-apis/src/test/java/com/baeldung/FileWriterExampleUnitTest.java index d8bc22b554..8089b32b2e 100644 --- a/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/filewriter/FileWriterExampleUnitTest.java +++ b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/FileWriterExampleUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.filewriter; +package com.baeldung; import org.junit.After; import org.junit.Assert; diff --git a/core-java-modules/core-java-io/pom.xml b/core-java-modules/core-java-io/pom.xml index 7de29ac23c..e645534b46 100644 --- a/core-java-modules/core-java-io/pom.xml +++ b/core-java-modules/core-java-io/pom.xml @@ -38,6 +38,12 @@ fscontext ${fscontext.version} + + javax.activation + activation + 1.1 + test + diff --git a/core-java-modules/core-java-io/src/test/resources/sampleTextFile.txt b/core-java-modules/core-java-io/src/test/resources/sampleTextFile.txt index 75cb50aafa..f9264f7fbd 100644 --- a/core-java-modules/core-java-io/src/test/resources/sampleTextFile.txt +++ b/core-java-modules/core-java-io/src/test/resources/sampleTextFile.txt @@ -1,2 +1,2 @@ -Hello -World \ No newline at end of file +Hello +World