Add Java-io-2

This commit is contained in:
YuCheng Hu 2023-09-03 10:31:14 -04:00 committed by honeymoose
parent e21d6d2e93
commit a68493c2ec
33 changed files with 1189 additions and 0 deletions

View File

@ -0,0 +1,5 @@
0.*
# Files generated by integration tests
# *.txt
/temp

View File

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

View File

@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-io-2</artifactId>
<version>${project.parent.version}</version>
<name>core-java-io-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.ossez.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<!-- utils -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${log4j-over-slf4j.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.tomakehurst/wiremock -->
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-io-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<source>11</source>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven-javadoc-plugin.version>3.3.2</maven-javadoc-plugin.version>
<wiremock.version>2.26.3</wiremock.version>
</properties>
</project>

View File

@ -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<String> listFilesUsingJavaIO(String dir) {
return Stream.of(new File(dir).listFiles())
.filter(file -> !file.isDirectory())
.map(File::getName)
.collect(Collectors.toSet());
}
public Set<String> listFilesUsingFilesList(String dir) throws IOException {
try (Stream<Path> stream = Files.list(Paths.get(dir))) {
return stream
.filter(file -> !Files.isDirectory(file))
.map(Path::getFileName)
.map(Path::toString)
.collect(Collectors.toSet());
}
}
public Set<String> listFilesUsingFileWalk(String dir, int depth) throws IOException {
try (Stream<Path> 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<String> listFilesUsingFileWalkAndVisitor(String dir) throws IOException {
Set<String> fileList = new HashSet<>();
Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (!Files.isDirectory(file)) {
fileList.add(file.getFileName().toString());
}
return FileVisitResult.CONTINUE;
}
});
return fileList;
}
public Set<String> listFilesUsingDirectoryStream(String dir) throws IOException {
Set<String> fileSet = new HashSet<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir))) {
for (Path path : stream) {
if (!Files.isDirectory(path)) {
fileSet.add(path.getFileName().toString());
}
}
}
return fileSet;
}
}

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="15 seconds" debug="false">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<String> EXPECTED_FILE_SET = new HashSet<String>() {
{
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<String> expectedFileSet = Collections.singleton("test.xml");
String filePathString = "src/test/resources/listFilesUnitTestFolder/test.xml";
assertEquals(expectedFileSet, listFiles.listFilesUsingFileWalk(filePathString, DEPTH));
}
}

View File

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

View File

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

View File

@ -0,0 +1,3 @@
UK
US
Germany

View File

@ -0,0 +1 @@
This example shows how we can delete the file contents without deleting the file

View File

@ -0,0 +1 @@
This is a sample txt file for unit test ListFilesUnitTest

View File

@ -0,0 +1 @@
Some StringProduct name is iPhone and its price is 1000 $

View File

@ -0,0 +1 @@
Hello World