diff --git a/core-java-modules/core-java-io-4/src/main/java/com/baeldung/iostreams/DataStream.java b/core-java-modules/core-java-io-4/src/main/java/com/baeldung/iostreams/DataStream.java new file mode 100644 index 0000000000..71c55d31b9 --- /dev/null +++ b/core-java-modules/core-java-io-4/src/main/java/com/baeldung/iostreams/DataStream.java @@ -0,0 +1,37 @@ +package com.baeldung.iostreams; + +import java.io.*; + +public class DataStream { + public static void textDataProcessingByteStream(String fileName, String content) throws IOException { + PrintStream out = new PrintStream(fileName); + out.print(content); + out.flush(); + } + + public static void textDataProcessingCharStream(String fileName, String content) throws IOException { + PrintWriter out = new PrintWriter(fileName); + out.print(content); + out.flush(); + } + + public static void nonTextDataProcessing(String fileName, String streamOutputFile, String writerOutputFile) throws IOException { + FileInputStream inputStream = new FileInputStream(fileName); + PrintStream printStream = new PrintStream(streamOutputFile); + + int b; + while ((b = inputStream.read()) != -1) { + printStream.write(b); + } + printStream.close(); + + FileReader reader = new FileReader(fileName); + PrintWriter writer = new PrintWriter(writerOutputFile); + + int c; + while ((c = reader.read()) != -1) { + writer.write(c); + } + writer.close(); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-io-4/src/test/java/com/baeldung/iostreams/DataStreamUnitTest.java b/core-java-modules/core-java-io-4/src/test/java/com/baeldung/iostreams/DataStreamUnitTest.java new file mode 100644 index 0000000000..a5a53d5a08 --- /dev/null +++ b/core-java-modules/core-java-io-4/src/test/java/com/baeldung/iostreams/DataStreamUnitTest.java @@ -0,0 +1,66 @@ +package com.baeldung.iostreams; + +import org.apache.commons.io.FileUtils; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class DataStreamUnitTest { + + private static final String dataProcessingTextFile = "src/test/resources/iostreams/TestFile.txt"; + private static final String dataProcessingImageFile = "src/test/resources/iostreams/image.png"; + private static final String dataProcessingByteStreamFile = "src/test/resources/iostreams/ps.png"; + private static final String dataProcessingCharStreamFile = "src/test/resources/iostreams/pw.png"; + + private static final String textFileContent = "Hello, world!"; + + @Test + public void whenUsingByteStream_thenWriteTextToFile() throws IOException { + DataStream dataStream = new DataStream(); + dataStream.textDataProcessingByteStream(dataProcessingTextFile, textFileContent); + + File file = new File(dataProcessingTextFile); + assertTrue(file.exists()); + assertEquals(textFileContent, FileUtils.readFileToString(file, "utf-8")); + + Files.delete(Paths.get(dataProcessingTextFile)); + } + + @Test + public void whenUsingCharStream_thenWriteTextToFile() throws IOException { + DataStream dataStream = new DataStream(); + dataStream.textDataProcessingCharStream(dataProcessingTextFile, textFileContent); + + File file = new File(dataProcessingTextFile); + assertTrue(file.exists()); + assertEquals(textFileContent, FileUtils.readFileToString(file, "utf-8")); + + Files.delete(Paths.get(dataProcessingTextFile)); + } + + @Test + public void whenUsingStreams_thenWriteNonTextData() throws IOException { + DataStream dataStream = new DataStream(); + dataStream.nonTextDataProcessing(dataProcessingImageFile, dataProcessingByteStreamFile, dataProcessingCharStreamFile); + + File file = new File(dataProcessingImageFile); + File byteStreamOutputFile = new File(dataProcessingByteStreamFile); + File charStreamOutputFile = new File(dataProcessingCharStreamFile); + assertTrue(file.exists()); + assertTrue(byteStreamOutputFile.exists()); + assertTrue(charStreamOutputFile.exists()); + + assertTrue(FileUtils.contentEquals(file, byteStreamOutputFile)); + assertFalse(FileUtils.contentEquals(file, charStreamOutputFile)); + + Files.delete(Paths.get(dataProcessingByteStreamFile)); + Files.delete(Paths.get(dataProcessingCharStreamFile)); + } +} diff --git a/core-java-modules/core-java-io-4/src/test/resources/iostreams/image.png b/core-java-modules/core-java-io-4/src/test/resources/iostreams/image.png new file mode 100644 index 0000000000..6ac7f37ece Binary files /dev/null and b/core-java-modules/core-java-io-4/src/test/resources/iostreams/image.png differ