diff --git a/core-java-modules/core-java-io-2/.gitignore b/core-java-modules/core-java-io-2/.gitignore
new file mode 100644
index 0000000000..c61d35324d
--- /dev/null
+++ b/core-java-modules/core-java-io-2/.gitignore
@@ -0,0 +1,5 @@
+0.*
+
+# Files generated by integration tests
+# *.txt
+/temp
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/README.md b/core-java-modules/core-java-io-2/README.md
new file mode 100644
index 0000000000..b078a66a7a
--- /dev/null
+++ b/core-java-modules/core-java-io-2/README.md
@@ -0,0 +1,16 @@
+## Core Java IO
+
+This module contains articles about core Java input and output (IO)
+
+### Relevant Articles:
+- [Create a File in a Specific Directory in Java](https://www.baeldung.com/java-create-file-in-directory)
+- [How to Read a Large File Efficiently with Java](https://www.baeldung.com/java-read-lines-large-file)
+- [Java – Write to File](https://www.baeldung.com/java-write-to-file)
+- [FileNotFoundException in Java](https://www.baeldung.com/java-filenotfound-exception)
+- [Delete the Contents of a File in Java](https://www.baeldung.com/java-delete-file-contents)
+- [List Files in a Directory in Java](https://www.baeldung.com/java-list-directory-files)
+- [Java – Append Data to a File](https://www.baeldung.com/java-append-to-file)
+- [How to Copy a File with Java](https://www.baeldung.com/java-copy-file)
+- [Create a Directory in Java](https://www.baeldung.com/java-create-directory)
+- [Java IO vs NIO](https://www.baeldung.com/java-io-vs-nio)
+- [[<-- Prev]](/core-java-modules/core-java-io)[[More -->]](/core-java-modules/core-java-io-3)
diff --git a/core-java-modules/core-java-io-2/pom.xml b/core-java-modules/core-java-io-2/pom.xml
new file mode 100644
index 0000000000..67bcaf5e16
--- /dev/null
+++ b/core-java-modules/core-java-io-2/pom.xml
@@ -0,0 +1,70 @@
+
+
+ 4.0.0
+ core-java-io-2
+ ${project.parent.version}
+ core-java-io-2
+ jar
+
+
+ com.ossez.core-java-modules
+ core-java-modules
+ 0.0.2-SNAPSHOT
+ ../pom.xml
+
+
+
+
+
+ commons-io
+ commons-io
+ ${commons-io.version}
+
+
+
+ log4j
+ log4j
+ ${log4j.version}
+
+
+ org.slf4j
+ log4j-over-slf4j
+ ${log4j-over-slf4j.version}
+
+
+
+ com.github.tomakehurst
+ wiremock
+ ${wiremock.version}
+ test
+
+
+
+
+ core-java-io-2
+
+
+ src/main/resources
+ true
+
+
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ ${maven-javadoc-plugin.version}
+
+ 11
+
+
+
+
+
+
+ 3.3.2
+ 2.26.3
+
+
+
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/src/main/java/com/ossez/listfiles/ListFiles.java b/core-java-modules/core-java-io-2/src/main/java/com/ossez/listfiles/ListFiles.java
new file mode 100644
index 0000000000..a16a634d85
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/main/java/com/ossez/listfiles/ListFiles.java
@@ -0,0 +1,66 @@
+package com.ossez.listfiles;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.*;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class ListFiles {
+
+ public Set listFilesUsingJavaIO(String dir) {
+ return Stream.of(new File(dir).listFiles())
+ .filter(file -> !file.isDirectory())
+ .map(File::getName)
+ .collect(Collectors.toSet());
+ }
+
+ public Set listFilesUsingFilesList(String dir) throws IOException {
+ try (Stream stream = Files.list(Paths.get(dir))) {
+ return stream
+ .filter(file -> !Files.isDirectory(file))
+ .map(Path::getFileName)
+ .map(Path::toString)
+ .collect(Collectors.toSet());
+ }
+ }
+
+ public Set listFilesUsingFileWalk(String dir, int depth) throws IOException {
+ try (Stream stream = Files.walk(Paths.get(dir), depth)) {
+ return stream
+ .filter(file -> !Files.isDirectory(file))
+ .map(Path::getFileName)
+ .map(Path::toString)
+ .collect(Collectors.toSet());
+ }
+ }
+
+ public Set listFilesUsingFileWalkAndVisitor(String dir) throws IOException {
+ Set fileList = new HashSet<>();
+ Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor() {
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
+ if (!Files.isDirectory(file)) {
+ fileList.add(file.getFileName().toString());
+ }
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ return fileList;
+ }
+
+ public Set listFilesUsingDirectoryStream(String dir) throws IOException {
+ Set fileSet = new HashSet<>();
+ try (DirectoryStream stream = Files.newDirectoryStream(Paths.get(dir))) {
+ for (Path path : stream) {
+ if (!Files.isDirectory(path)) {
+ fileSet.add(path.getFileName().toString());
+ }
+ }
+ }
+ return fileSet;
+ }
+}
diff --git a/core-java-modules/core-java-io-2/src/main/resources/logback.xml b/core-java-modules/core-java-io-2/src/main/resources/logback.xml
new file mode 100644
index 0000000000..f46a64b3cb
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/main/resources/logback.xml
@@ -0,0 +1,12 @@
+
+
+
+
+ [%d{ISO8601}]-[%thread] %-5level %logger - %msg%n
+
+
+
+
+
+
+
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/appendtofile/AppendToFileManualTest.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/appendtofile/AppendToFileManualTest.java
new file mode 100644
index 0000000000..62ef8d0215
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/appendtofile/AppendToFileManualTest.java
@@ -0,0 +1,80 @@
+package com.ossez.appendtofile;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.CharSink;
+import com.google.common.io.FileWriteMode;
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class AppendToFileManualTest {
+
+ public static final String fileName = "src/main/resources/countries.properties";
+
+ @Before
+ @After
+ public void setup() throws Exception {
+ PrintWriter writer = new PrintWriter(fileName);
+ writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
+ writer.close();
+ }
+
+ @Test
+ public void whenAppendToFileUsingGuava_thenCorrect() throws IOException {
+ File file = new File(fileName);
+ CharSink chs = com.google.common.io.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");
+ }
+
+ @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");
+ }
+
+ @Test
+ public void whenAppendToFileUsingFileUtils_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");
+ }
+
+ @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");
+ }
+
+ @Test
+ public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
+ 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");
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/appendtofile/StreamUtils.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/appendtofile/StreamUtils.java
new file mode 100644
index 0000000000..6ab7f89a44
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/appendtofile/StreamUtils.java
@@ -0,0 +1,16 @@
+package com.ossez.appendtofile;
+
+import org.apache.commons.io.IOUtils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+
+public class StreamUtils {
+
+ public static String getStringFromInputStream(InputStream input) throws IOException {
+ StringWriter writer = new StringWriter();
+ IOUtils.copy(input, writer, "UTF-8");
+ return writer.toString();
+ }
+}
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/blockingnonblocking/BlockingClientUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/blockingnonblocking/BlockingClientUnitTest.java
new file mode 100644
index 0000000000..c667a42e7d
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/blockingnonblocking/BlockingClientUnitTest.java
@@ -0,0 +1,52 @@
+package com.ossez.blockingnonblocking;
+
+import com.github.tomakehurst.wiremock.junit.WireMockRule;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import java.io.*;
+import java.net.Socket;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
+import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
+import static org.junit.Assert.assertTrue;
+
+public class BlockingClientUnitTest {
+ private static final String REQUESTED_RESOURCE = "/test.json";
+
+ @Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
+
+ @Before
+ public void setup() {
+ stubFor(get(urlEqualTo(REQUESTED_RESOURCE)).willReturn(aResponse()
+ .withStatus(200)
+ .withBody("{ \"response\" : \"It worked!\" }\r\n\r\n")));
+ }
+
+ @Test
+ public void givenJavaIOSocket_whenReadingAndWritingWithStreams_thenSuccess() throws IOException {
+ // given an IO socket and somewhere to store our result
+ Socket socket = new Socket("localhost", wireMockRule.port());
+ StringBuilder ourStore = new StringBuilder();
+
+ // when we write and read (using try-with-resources so our resources are auto-closed)
+ try (InputStream serverInput = socket.getInputStream();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput));
+ OutputStream clientOutput = socket.getOutputStream();
+ PrintWriter writer = new PrintWriter(new OutputStreamWriter(clientOutput))) {
+ writer.print("GET " + REQUESTED_RESOURCE + " HTTP/1.0\r\n\r\n");
+ writer.flush(); // important - without this the request is never sent, and the test will hang on readLine()
+
+ for (String line; (line = reader.readLine()) != null; ) {
+ ourStore.append(line);
+ ourStore.append(System.lineSeparator());
+ }
+ }
+
+ // then we read and saved our data
+ assertTrue(ourStore
+ .toString()
+ .contains("It worked!"));
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/blockingnonblocking/NonBlockingClientUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/blockingnonblocking/NonBlockingClientUnitTest.java
new file mode 100644
index 0000000000..d7ae5e4a06
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/blockingnonblocking/NonBlockingClientUnitTest.java
@@ -0,0 +1,94 @@
+package com.ossez.blockingnonblocking;
+
+import com.github.tomakehurst.wiremock.junit.WireMockRule;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.channels.SocketChannel;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.StandardCharsets;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
+import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
+import static org.junit.Assert.assertTrue;
+
+public class NonBlockingClientUnitTest {
+ private String REQUESTED_RESOURCE = "/test.json";
+
+ @Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
+
+ @Before
+ public void setup() {
+ stubFor(get(urlEqualTo(REQUESTED_RESOURCE)).willReturn(aResponse()
+ .withStatus(200)
+ .withBody("{ \"response\" : \"It worked!\" }")));
+ }
+
+ @Test
+ public void givenJavaNIOSocketChannel_whenReadingAndWritingWithBuffers_thenSuccess() throws IOException {
+ // given a NIO SocketChannel and a charset
+ InetSocketAddress address = new InetSocketAddress("localhost", wireMockRule.port());
+ SocketChannel socketChannel = SocketChannel.open(address);
+ Charset charset = StandardCharsets.UTF_8;
+
+ // when we write and read using buffers
+ socketChannel.write(charset.encode(CharBuffer.wrap("GET " + REQUESTED_RESOURCE + " HTTP/1.0\r\n\r\n")));
+
+ ByteBuffer byteBuffer = ByteBuffer.allocate(8192); // or allocateDirect if we need direct memory access
+ CharBuffer charBuffer = CharBuffer.allocate(8192);
+ CharsetDecoder charsetDecoder = charset.newDecoder();
+ StringBuilder ourStore = new StringBuilder();
+ while (socketChannel.read(byteBuffer) != -1 || byteBuffer.position() > 0) {
+ byteBuffer.flip();
+ storeBufferContents(byteBuffer, charBuffer, charsetDecoder, ourStore);
+ byteBuffer.compact();
+ }
+ socketChannel.close();
+
+ // then we read and saved our data
+ assertTrue(ourStore
+ .toString()
+ .contains("It worked!"));
+ }
+
+ @Test
+ public void givenJavaNIOSocketChannel_whenReadingAndWritingWithSmallBuffers_thenSuccess() throws IOException {
+ // given a NIO SocketChannel and a charset
+ InetSocketAddress address = new InetSocketAddress("localhost", wireMockRule.port());
+ SocketChannel socketChannel = SocketChannel.open(address);
+ Charset charset = StandardCharsets.UTF_8;
+
+ // when we write and read using buffers that are too small for our message
+ socketChannel.write(charset.encode(CharBuffer.wrap("GET " + REQUESTED_RESOURCE + " HTTP/1.0\r\n\r\n")));
+
+ ByteBuffer byteBuffer = ByteBuffer.allocate(8); // or allocateDirect if we need direct memory access
+ CharBuffer charBuffer = CharBuffer.allocate(8);
+ CharsetDecoder charsetDecoder = charset.newDecoder();
+ StringBuilder ourStore = new StringBuilder();
+ while (socketChannel.read(byteBuffer) != -1 || byteBuffer.position() > 0) {
+ byteBuffer.flip();
+ storeBufferContents(byteBuffer, charBuffer, charsetDecoder, ourStore);
+ byteBuffer.compact();
+ }
+ socketChannel.close();
+
+ // then we read and saved our data
+ assertTrue(ourStore
+ .toString()
+ .contains("It worked!"));
+ }
+
+ void storeBufferContents(ByteBuffer byteBuffer, CharBuffer charBuffer, CharsetDecoder charsetDecoder, StringBuilder ourStore) {
+ charsetDecoder.decode(byteBuffer, charBuffer, true);
+ charBuffer.flip();
+ ourStore.append(charBuffer);
+ charBuffer.clear();
+ }
+
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/copyfiles/FileCopierIntegrationTest.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/copyfiles/FileCopierIntegrationTest.java
new file mode 100644
index 0000000000..4513aeef47
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/copyfiles/FileCopierIntegrationTest.java
@@ -0,0 +1,69 @@
+package com.ossez.copyfiles;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Before;
+import org.junit.Test;
+import static org.assertj.core.api.Assertions.*;
+
+public class FileCopierIntegrationTest {
+ File original = new File("src/test/resources/original.txt");
+
+ @Before
+ public void init() throws IOException {
+ if (!original.exists())
+ Files.createFile(original.toPath());
+ }
+
+ @Test
+ public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
+ File copied = new File("src/test/resources/copiedWithIo.txt");
+ try (InputStream in = new BufferedInputStream(new FileInputStream(original)); OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) {
+ byte[] buffer = new byte[1024];
+ int lengthRead;
+ while ((lengthRead = in.read(buffer)) > 0) {
+ out.write(buffer, 0, lengthRead);
+ out.flush();
+ }
+ }
+ assertThat(copied).exists();
+ assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
+ }
+
+ @Test
+ public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
+ File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
+ FileUtils.copyFile(original, copied);
+ assertThat(copied).exists();
+ assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
+ }
+
+ @Test
+ public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException {
+ Path copied = Paths.get("src/test/resources/copiedWithNio.txt");
+ Path originalPath = original.toPath();
+ Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING);
+ assertThat(copied).exists();
+ assertThat(Files.readAllLines(originalPath).equals(Files.readAllLines(copied)));
+ }
+
+ @Test
+ public void givenGuava_whenCopied_thenCopyExistsWithSameContents() throws IOException {
+ File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
+ com.google.common.io.Files.copy(original, copied);
+ assertThat(copied).exists();
+ assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
+ }
+}
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/createfiles/CreateFilesUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/createfiles/CreateFilesUnitTest.java
new file mode 100644
index 0000000000..55461569a9
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/createfiles/CreateFilesUnitTest.java
@@ -0,0 +1,60 @@
+package com.ossez.createfiles;
+
+import com.google.common.io.Files;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import org.junit.BeforeClass;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class CreateFilesUnitTest {
+
+ @BeforeClass
+ public static void clean() {
+ File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
+ File file1 = new File(tempDirectory.getAbsolutePath() + "/testFile.txt");
+ File file2 = new File(tempDirectory, "newFile.txt");
+ File file3 = new File(tempDirectory.getAbsolutePath() + File.separator + "newFile2.txt");
+ file1.delete();
+ file2.delete();
+ file3.delete();
+ }
+
+ @Test
+ public void givenAnExistingDirectory_whenCreatingAFileWithAbsolutePath_thenFileIsCreated() throws IOException {
+ File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
+ File fileWithAbsolutePath = new File(tempDirectory.getAbsolutePath() + "/testFile.txt");
+
+ assertFalse(fileWithAbsolutePath.exists());
+
+ Files.touch(fileWithAbsolutePath);
+
+ assertTrue(fileWithAbsolutePath.exists());
+ }
+
+ @Test
+ public void givenAnExistingDirectory_whenCreatingANewDirectoryAndFileWithRelativePath_thenFileIsCreated() throws IOException {
+ File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
+ File fileWithRelativePath = new File(tempDirectory, "newFile.txt");
+
+ assertFalse(fileWithRelativePath.exists());
+
+ Files.touch(fileWithRelativePath);
+
+ assertTrue(fileWithRelativePath.exists());
+ }
+
+ @Test
+ public void whenCreatingAFileWithFileSeparator_thenFileIsCreated() throws IOException {
+ File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
+ File newFile = new File(tempDirectory.getAbsolutePath() + File.separator + "newFile2.txt");
+
+ assertFalse(newFile.exists());
+
+ Files.touch(newFile);
+
+ assertTrue(newFile.exists());
+ }
+}
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/deletecontents/FilesClearDataUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/deletecontents/FilesClearDataUnitTest.java
new file mode 100644
index 0000000000..a241bbe377
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/deletecontents/FilesClearDataUnitTest.java
@@ -0,0 +1,94 @@
+package com.ossez.deletecontents;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.channels.FileChannel;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class FilesClearDataUnitTest {
+
+ public static final String FILE_PATH = "src/test/resources/fileexample.txt";
+
+ @Before
+ @After
+ public void setup() throws IOException {
+ PrintWriter writer = new PrintWriter(FILE_PATH);
+ writer.print("This example shows how we can delete the file contents without deleting the file");
+ writer.close();
+ }
+
+ @Test
+ public void givenExistingFile_whenDeleteContentUsingPrintWritter_thenEmptyFile() throws IOException {
+ PrintWriter writer = new PrintWriter(FILE_PATH);
+ writer.print("");
+ writer.close();
+ assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
+ }
+
+ @Test
+ public void givenExistingFile_whenDeleteContentUsingPrintWritterWithougObject_thenEmptyFile() throws IOException {
+ new PrintWriter(FILE_PATH).close();
+ assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
+ }
+
+ @Test
+ public void givenExistingFile_whenDeleteContentUsingFileWriter_thenEmptyFile() throws IOException {
+ new FileWriter(FILE_PATH, false).close();
+
+ assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
+ }
+
+ @Test
+ public void givenExistingFile_whenDeleteContentUsingFileOutputStream_thenEmptyFile() throws IOException {
+ new FileOutputStream(FILE_PATH).close();
+
+ assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
+ }
+
+ @Test
+ public void givenExistingFile_whenDeleteContentUsingFileUtils_thenEmptyFile() throws IOException {
+ FileUtils.write(new File(FILE_PATH), "", Charset.defaultCharset());
+
+ assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
+ }
+
+ @Test
+ public void givenExistingFile_whenDeleteContentUsingNIOFiles_thenEmptyFile() throws IOException {
+ BufferedWriter writer = Files.newBufferedWriter(Paths.get(FILE_PATH));
+ writer.write("");
+ writer.flush();
+
+ assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
+ }
+
+ @Test
+ public void givenExistingFile_whenDeleteContentUsingNIOFileChannel_thenEmptyFile() throws IOException {
+ FileChannel.open(Paths.get(FILE_PATH), StandardOpenOption.WRITE).truncate(0).close();
+
+ assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
+ }
+
+ @Test
+ public void givenExistingFile_whenDeleteContentUsingGuava_thenEmptyFile() throws IOException {
+ File file = new File(FILE_PATH);
+ byte[] empty = new byte[0];
+ com.google.common.io.Files.write(empty, file);
+
+ assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
+ }
+}
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/deletecontents/StreamUtils.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/deletecontents/StreamUtils.java
new file mode 100644
index 0000000000..0836a4f607
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/deletecontents/StreamUtils.java
@@ -0,0 +1,16 @@
+package com.ossez.deletecontents;
+
+import org.apache.commons.io.IOUtils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+
+public class StreamUtils {
+
+ public static String getStringFromInputStream(InputStream input) throws IOException {
+ StringWriter writer = new StringWriter();
+ IOUtils.copy(input, writer, "UTF-8");
+ return writer.toString();
+ }
+}
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/directories/NewDirectoryUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/directories/NewDirectoryUnitTest.java
new file mode 100644
index 0000000000..51fe59e9cd
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/directories/NewDirectoryUnitTest.java
@@ -0,0 +1,100 @@
+package com.ossez.directories;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.File;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class NewDirectoryUnitTest {
+
+ private static final File TEMP_DIRECTORY = new File(System.getProperty("java.io.tmpdir"));
+
+ @Before
+ public void beforeEach() {
+ File newDirectory = new File(TEMP_DIRECTORY, "new_directory");
+ File nestedInNewDirectory = new File(newDirectory, "nested_directory");
+ File existingDirectory = new File(TEMP_DIRECTORY, "existing_directory");
+ File existingNestedDirectory = new File(existingDirectory, "existing_nested_directory");
+ File nestedInExistingDirectory = new File(existingDirectory, "nested_directory");
+
+ nestedInNewDirectory.delete();
+ newDirectory.delete();
+ nestedInExistingDirectory.delete();
+ existingDirectory.mkdir();
+ existingNestedDirectory.mkdir();
+ }
+
+ @Test
+ public void givenUnexistingDirectory_whenMkdir_thenTrue() {
+ File newDirectory = new File(TEMP_DIRECTORY, "new_directory");
+ assertFalse(newDirectory.exists());
+
+ boolean directoryCreated = newDirectory.mkdir();
+
+ assertTrue(directoryCreated);
+ }
+
+ @Test
+ public void givenExistingDirectory_whenMkdir_thenFalse() {
+ File newDirectory = new File(TEMP_DIRECTORY, "new_directory");
+ newDirectory.mkdir();
+ assertTrue(newDirectory.exists());
+
+ boolean directoryCreated = newDirectory.mkdir();
+
+ assertFalse(directoryCreated);
+ }
+
+ @Test
+ public void givenUnexistingNestedDirectories_whenMkdir_thenFalse() {
+ File newDirectory = new File(TEMP_DIRECTORY, "new_directory");
+ File nestedDirectory = new File(newDirectory, "nested_directory");
+ assertFalse(newDirectory.exists());
+ assertFalse(nestedDirectory.exists());
+
+ boolean directoriesCreated = nestedDirectory.mkdir();
+
+ assertFalse(directoriesCreated);
+ }
+
+ @Test
+ public void givenUnexistingNestedDirectories_whenMkdirs_thenTrue() {
+ File newDirectory = new File(System.getProperty("java.io.tmpdir") + File.separator + "new_directory");
+ File nestedDirectory = new File(newDirectory, "nested_directory");
+ assertFalse(newDirectory.exists());
+ assertFalse(nestedDirectory.exists());
+
+ boolean directoriesCreated = nestedDirectory.mkdirs();
+
+ assertTrue(directoriesCreated);
+ }
+
+ @Test
+ public void givenExistingParentDirectories_whenMkdirs_thenTrue() {
+ File newDirectory = new File(TEMP_DIRECTORY, "existing_directory");
+ newDirectory.mkdir();
+ File nestedDirectory = new File(newDirectory, "nested_directory");
+ assertTrue(newDirectory.exists());
+ assertFalse(nestedDirectory.exists());
+
+ boolean directoriesCreated = nestedDirectory.mkdirs();
+
+ assertTrue(directoriesCreated);
+ }
+
+ @Test
+ public void givenExistingNestedDirectories_whenMkdirs_thenFalse() {
+ File existingDirectory = new File(TEMP_DIRECTORY, "existing_directory");
+ File existingNestedDirectory = new File(existingDirectory, "existing_nested_directory");
+ assertTrue(existingDirectory.exists());
+ assertTrue(existingNestedDirectory.exists());
+
+ boolean directoriesCreated = existingNestedDirectory.mkdirs();
+
+ assertFalse(directoriesCreated);
+ }
+
+}
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/filenotfoundexception/FileNotFoundExceptionUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/filenotfoundexception/FileNotFoundExceptionUnitTest.java
new file mode 100644
index 0000000000..8db6d55131
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/filenotfoundexception/FileNotFoundExceptionUnitTest.java
@@ -0,0 +1,58 @@
+package com.ossez.filenotfoundexception;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.*;
+
+public class FileNotFoundExceptionUnitTest {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FileNotFoundExceptionUnitTest.class);
+
+ private String fileName = Double.toString(Math.random());
+
+ @Test(expected = BusinessException.class)
+ public void raiseBusinessSpecificException() throws IOException {
+ try {
+ readFailingFile();
+ } catch (FileNotFoundException ex) {
+ throw new BusinessException("BusinessException: necessary file was not present.");
+ }
+ }
+
+ @Test
+ public void createFile() throws IOException {
+ try {
+ readFailingFile();
+ } catch (FileNotFoundException ex) {
+ try {
+ new File(fileName).createNewFile();
+ readFailingFile();
+ } catch (IOException ioe) {
+ throw new RuntimeException("BusinessException: even creation is not possible.");
+ }
+ }
+ }
+
+ @Test
+ public void logError() throws IOException {
+ try {
+ readFailingFile();
+ } catch (FileNotFoundException ex) {
+ LOG.error("Optional file " + fileName + " was not found.");
+ }
+ }
+
+ private void readFailingFile() throws IOException {
+ BufferedReader rd = new BufferedReader(new FileReader(new File(fileName)));
+ rd.readLine();
+ // no need to close file
+ }
+
+ private class BusinessException extends RuntimeException {
+ BusinessException(String string) {
+ super(string);
+ }
+ }
+}
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/listfiles/ListFilesUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/listfiles/ListFilesUnitTest.java
new file mode 100644
index 0000000000..4e75588c88
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/listfiles/ListFilesUnitTest.java
@@ -0,0 +1,71 @@
+package com.ossez.listfiles;
+
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class ListFilesUnitTest {
+
+ private static final String VALID_DIRECTORY = "src/test/resources/listFilesUnitTestFolder";
+ private static final String INVALID_DIRECTORY = "src/test/resources/thisDirectoryDoesNotExist";
+ private static final Set EXPECTED_FILE_SET = new HashSet() {
+ {
+ add("test.xml");
+ add("employee.json");
+ add("students.json");
+ add("country.txt");
+ }
+ };
+ private static final int DEPTH = 1;
+ private final ListFiles listFiles = new ListFiles();
+
+ @Test
+ public void givenAValidDirectoryWhenUsingJavaIOThenReturnSetOfFileNames() {
+ assertThat(listFiles.listFilesUsingJavaIO(VALID_DIRECTORY))
+ .isEqualTo(EXPECTED_FILE_SET);
+ }
+
+ @Test
+ public void givenAInvalidValidDirectoryWhenUsingJavaIOThenThrowsNullPointerExceptino() {
+ assertThrows(NullPointerException.class,
+ () -> listFiles.listFilesUsingJavaIO(INVALID_DIRECTORY));
+ }
+
+ @Test
+ public void givenAValidDirectoryWhenUsingFilesListThenReturnSetOfFileNames() throws IOException {
+ assertThat(listFiles.listFilesUsingFilesList(VALID_DIRECTORY))
+ .isEqualTo(EXPECTED_FILE_SET);
+ }
+
+ @Test
+ public void givenAValidDirectoryWhenUsingFilesWalkWithDepth1ThenReturnSetOfFileNames() throws IOException {
+ assertThat(listFiles.listFilesUsingFileWalk(VALID_DIRECTORY, DEPTH))
+ .isEqualTo(EXPECTED_FILE_SET);
+ }
+
+ @Test
+ public void givenAValidDirectoryWhenUsingFilesWalkFileTreeThenReturnSetOfFileNames() throws IOException {
+ assertThat(listFiles.listFilesUsingFileWalkAndVisitor(VALID_DIRECTORY))
+ .isEqualTo(EXPECTED_FILE_SET);
+ }
+
+ @Test
+ public void givenAValidDirectoryWhenUsingDirectoryStreamThenReturnSetOfFileNames() throws IOException {
+ assertThat(listFiles.listFilesUsingDirectoryStream(VALID_DIRECTORY))
+ .isEqualTo(EXPECTED_FILE_SET);
+ }
+
+ @Test
+ public void givenAValidFileWhenUsingFilesWalkWithDepth1ThenReturnSetOfTheSameFile() throws IOException {
+ Set expectedFileSet = Collections.singleton("test.xml");
+ String filePathString = "src/test/resources/listFilesUnitTestFolder/test.xml";
+ assertEquals(expectedFileSet, listFiles.listFilesUsingFileWalk(filePathString, DEPTH));
+ }
+}
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/readlargefile/ReadLargeFileUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/readlargefile/ReadLargeFileUnitTest.java
new file mode 100644
index 0000000000..52079f23ef
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/readlargefile/ReadLargeFileUnitTest.java
@@ -0,0 +1,107 @@
+package com.ossez.readlargefile;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Scanner;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.LineIterator;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.Files;
+
+@Ignore("need large file for testing")
+public class ReadLargeFileUnitTest {
+ protected final Logger logger = LoggerFactory.getLogger(getClass());
+
+ // tests - iterate lines in a file
+
+ @Test
+ public final void givenUsingGuava_whenIteratingAFile_thenCorrect() throws IOException {
+ final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv";
+ // final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv";
+
+ logMemory();
+ Files.readLines(new File(path), Charsets.UTF_8);
+ logMemory();
+ }
+
+ @Test
+ public final void givenUsingCommonsIo_whenIteratingAFileInMemory_thenCorrect() throws IOException {
+ // final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv";
+ final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv";
+
+ logMemory();
+ FileUtils.readLines(new File(path));
+ logMemory();
+ }
+
+ @Test
+ public final void whenStreamingThroughAFile_thenCorrect() throws IOException {
+ final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv";
+ // final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv";
+
+ logMemory();
+
+ FileInputStream inputStream = null;
+ Scanner sc = null;
+ try {
+ inputStream = new FileInputStream(path);
+ sc = new Scanner(inputStream, "UTF-8");
+ while (sc.hasNextLine()) {
+ final String line = sc.nextLine();
+ // System.out.println(line);
+ }
+ // note that Scanner suppresses exceptions
+ if (sc.ioException() != null) {
+ throw sc.ioException();
+ }
+ } finally {
+ if (inputStream != null) {
+ inputStream.close();
+ }
+ if (sc != null) {
+ sc.close();
+ }
+ }
+
+ logMemory();
+ }
+
+ @Test
+ public final void givenUsingApacheIo_whenStreamingThroughAFile_thenCorrect() throws IOException {
+ final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv";
+ // final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv";
+
+ logMemory();
+
+ final LineIterator it = FileUtils.lineIterator(new File(path), "UTF-8");
+ try {
+ while (it.hasNext()) {
+ final String line = it.nextLine();
+ // do something with line
+ }
+ } finally {
+ LineIterator.closeQuietly(it);
+ }
+
+ logMemory();
+ }
+
+ // utils
+
+ private final void logMemory() {
+ logger.info("Max Memory: {} Mb", Runtime.getRuntime()
+ .maxMemory() / 1048576);
+ logger.info("Total Memory: {} Mb", Runtime.getRuntime()
+ .totalMemory() / 1048576);
+ logger.info("Free Memory: {} Mb", Runtime.getRuntime()
+ .freeMemory() / 1048576);
+ }
+
+}
diff --git a/core-java-modules/core-java-io-2/src/test/java/com/ossez/writetofile/JavaWriteToFileUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/ossez/writetofile/JavaWriteToFileUnitTest.java
new file mode 100644
index 0000000000..b2e1996fe8
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/java/com/ossez/writetofile/JavaWriteToFileUnitTest.java
@@ -0,0 +1,193 @@
+package com.ossez.writetofile;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
+import java.nio.channels.OverlappingFileLockException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.Test;
+
+public class JavaWriteToFileUnitTest {
+
+ private String fileNameLarge = "src/test/resources/test_large.txt";
+ private String fileName = "src/test/resources/test_write.txt";
+ private String fileName1 = "src/test/resources/test_write_1.txt";
+ private String fileName2 = "src/test/resources/test_write_2.txt";
+ private String fileName3 = "src/test/resources/test_write_3.txt";
+ private String fileName4 = "src/test/resources/test_write_4.txt";
+ private String fileName5 = "src/test/resources/test_write_5.txt";
+
+ @Test
+ public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException {
+ final String str = "Hello";
+ final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3));
+ writer.write(str);
+ writer.close();
+ }
+
+ @Test
+ public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException {
+ final String str = "World";
+ final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3, true));
+ writer.append(' ');
+ writer.append(str);
+ writer.close();
+ }
+
+ @Test
+ public void givenWritingStringToFile_whenUsingPrintWriter_thenCorrect() throws IOException {
+ final FileWriter fileWriter = new FileWriter(fileName);
+ final PrintWriter printWriter = new PrintWriter(fileWriter);
+ printWriter.print("Some String");
+ printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000);
+ printWriter.close();
+ }
+
+ @Test
+ public void givenWritingStringToFile_whenUsingFileOutputStream_thenCorrect() throws IOException {
+ final String str = "Hello";
+ final FileOutputStream outputStream = new FileOutputStream(fileName3);
+ final byte[] strToBytes = str.getBytes();
+ outputStream.write(strToBytes);
+ outputStream.close();
+ }
+
+ //
+
+ @Test
+ public void givenWritingToFile_whenUsingDataOutputStream_thenCorrect() throws IOException {
+ final String value = "Hello";
+ final FileOutputStream fos = new FileOutputStream(fileName1);
+ final DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
+ outStream.writeUTF(value);
+ outStream.close();
+
+ String result;
+ final FileInputStream fis = new FileInputStream(fileName1);
+ final DataInputStream reader = new DataInputStream(fis);
+ result = reader.readUTF();
+ reader.close();
+
+ assertEquals(value, result);
+ }
+
+ @Test
+ public void whenWritingToSpecificPositionInFile_thenCorrect() throws IOException {
+ final int data1 = 2014;
+ final int data2 = 1500;
+ writeToPosition(fileName2, data1, 4);
+ assertEquals(data1, readFromPosition(fileName2, 4));
+ writeToPosition(fileName2, data2, 4);
+ assertEquals(data2, readFromPosition(fileName2, 4));
+ }
+
+ @Test
+ public void whenTryToLockFile_thenItShouldBeLocked() throws IOException {
+ final RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
+ final FileChannel channel = stream.getChannel();
+
+ FileLock lock = null;
+ try {
+ lock = channel.tryLock();
+ } catch (final OverlappingFileLockException e) {
+ stream.close();
+ channel.close();
+ }
+ stream.writeChars("test lock");
+ lock.release();
+
+ stream.close();
+ channel.close();
+ }
+
+ @Test
+ public void givenWritingToFile_whenUsingFileChannel_thenCorrect() throws IOException {
+ final RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
+ final FileChannel channel = stream.getChannel();
+ final String value = "Hello";
+ final byte[] strBytes = value.getBytes();
+ final ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
+ buffer.put(strBytes);
+ buffer.flip();
+ channel.write(buffer);
+ stream.close();
+ channel.close();
+
+ final RandomAccessFile reader = new RandomAccessFile(fileName5, "r");
+ assertEquals(value, reader.readLine());
+ reader.close();
+
+ }
+
+ @Test
+ public void writingToLargeFile() throws IOException {
+ final RandomAccessFile stream = new RandomAccessFile(fileNameLarge, "rw");
+ stream.setLength(1024 * 1024 * 1024);
+ }
+
+ @Test
+ public void whenWriteToTmpFile_thenCorrect() throws IOException {
+ final String toWrite = "Hello";
+ final File tmpFile = File.createTempFile("test", ".tmp");
+ final FileWriter writer = new FileWriter(tmpFile);
+ writer.write(toWrite);
+ writer.close();
+
+ final BufferedReader reader = new BufferedReader(new FileReader(tmpFile));
+ assertEquals(toWrite, reader.readLine());
+ reader.close();
+ }
+
+ @Test
+ public void givenUsingJava7_whenWritingToFile_thenCorrect() throws IOException {
+ final String str = "Hello";
+
+ final Path path = Paths.get(fileName3);
+ final byte[] strToBytes = str.getBytes();
+
+ Files.write(path, strToBytes);
+
+ final String read = Files.readAllLines(path, Charset.defaultCharset()).get(0);
+ assertEquals(str, read);
+ }
+
+ // UTIL
+
+ // use RandomAccessFile to write data at specific position in the file
+ private void writeToPosition(final String filename, final int data, final long position) throws IOException {
+ final RandomAccessFile writer = new RandomAccessFile(filename, "rw");
+ writer.seek(position);
+ writer.writeInt(data);
+ writer.close();
+ }
+
+ // use RandomAccessFile to read data from specific position in the file
+ private int readFromPosition(final String filename, final long position) throws IOException {
+ int result = 0;
+ final RandomAccessFile reader = new RandomAccessFile(filename, "r");
+ reader.seek(position);
+ result = reader.readInt();
+ reader.close();
+ return result;
+ }
+
+}
diff --git a/core-java-modules/core-java-io-2/src/test/resources/copiedWithApacheCommons.txt b/core-java-modules/core-java-io-2/src/test/resources/copiedWithApacheCommons.txt
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/core-java-modules/core-java-io-2/src/test/resources/copiedWithIo.txt b/core-java-modules/core-java-io-2/src/test/resources/copiedWithIo.txt
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/core-java-modules/core-java-io-2/src/test/resources/copiedWithNio.txt b/core-java-modules/core-java-io-2/src/test/resources/copiedWithNio.txt
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/core-java-modules/core-java-io-2/src/test/resources/countries.properties b/core-java-modules/core-java-io-2/src/test/resources/countries.properties
new file mode 100644
index 0000000000..3c1f53aded
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/resources/countries.properties
@@ -0,0 +1,3 @@
+UK
+US
+Germany
diff --git a/core-java-modules/core-java-io-2/src/test/resources/fileexample.txt b/core-java-modules/core-java-io-2/src/test/resources/fileexample.txt
new file mode 100644
index 0000000000..ee48fdfb84
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/resources/fileexample.txt
@@ -0,0 +1 @@
+This example shows how we can delete the file contents without deleting the file
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/country.txt b/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/country.txt
new file mode 100644
index 0000000000..45bfe896dc
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/country.txt
@@ -0,0 +1 @@
+This is a sample txt file for unit test ListFilesUnitTest
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/employee.json b/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/employee.json
new file mode 100644
index 0000000000..9e26dfeeb6
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/employee.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/students.json b/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/students.json
new file mode 100644
index 0000000000..9e26dfeeb6
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/students.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/test.xml b/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/test.xml
new file mode 100644
index 0000000000..19b16cc72c
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/resources/listFilesUnitTestFolder/test.xml
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/src/test/resources/original.txt b/core-java-modules/core-java-io-2/src/test/resources/original.txt
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/core-java-modules/core-java-io-2/src/test/resources/test_write.txt b/core-java-modules/core-java-io-2/src/test/resources/test_write.txt
new file mode 100644
index 0000000000..a15aad69b5
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/resources/test_write.txt
@@ -0,0 +1 @@
+Some StringProduct name is iPhone and its price is 1000 $
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/src/test/resources/test_write_1.txt b/core-java-modules/core-java-io-2/src/test/resources/test_write_1.txt
new file mode 100644
index 0000000000..5727d54bfc
Binary files /dev/null and b/core-java-modules/core-java-io-2/src/test/resources/test_write_1.txt differ
diff --git a/core-java-modules/core-java-io-2/src/test/resources/test_write_2.txt b/core-java-modules/core-java-io-2/src/test/resources/test_write_2.txt
new file mode 100644
index 0000000000..6a87dfc075
Binary files /dev/null and b/core-java-modules/core-java-io-2/src/test/resources/test_write_2.txt differ
diff --git a/core-java-modules/core-java-io-2/src/test/resources/test_write_3.txt b/core-java-modules/core-java-io-2/src/test/resources/test_write_3.txt
new file mode 100644
index 0000000000..5e1c309dae
--- /dev/null
+++ b/core-java-modules/core-java-io-2/src/test/resources/test_write_3.txt
@@ -0,0 +1 @@
+Hello World
\ No newline at end of file
diff --git a/core-java-modules/core-java-io-2/src/test/resources/test_write_4.txt b/core-java-modules/core-java-io-2/src/test/resources/test_write_4.txt
new file mode 100644
index 0000000000..f14fca61f6
Binary files /dev/null and b/core-java-modules/core-java-io-2/src/test/resources/test_write_4.txt differ