BAEL-5822 printstream vs printwriter (#13199)

* BAEL-5822 pr with streams implementation, unit test and resource file for testing

* fix unit tests names

* swap arguments in assertion

Co-authored-by: Christian Jaimes <christian.jaimes@oracle>
This commit is contained in:
chrisjaimes 2023-01-09 05:52:25 -05:00 committed by GitHub
parent 0ef1591c3c
commit b6075009b0
3 changed files with 103 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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));
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 369 KiB