Split or move libraries-apache-commons module (#7873)

This commit is contained in:
Catalin Burcea
2019-09-27 17:36:49 +03:00
committed by Josh Cummings
parent 2a6a8024cd
commit eb6ced2100
24 changed files with 322 additions and 270 deletions

View File

@@ -0,0 +1,7 @@
## Apache Commons Collections
This module contains articles about Apache Commons IO
### Relevant articles
- [Apache Commons IO](https://www.baeldung.com/apache-commons-io)
- [Introduction to Apache Commons CSV](https://www.baeldung.com/apache-commons-csv)

View File

@@ -0,0 +1,31 @@
<?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>libraries-apache-commons-io</artifactId>
<name>libraries-apache-commons-io</name>
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>${commons-csv.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
</dependencies>
<properties>
<commons-csv.version>1.4</commons-csv.version>
</properties>
</project>

View File

@@ -0,0 +1,43 @@
package com.baeldung.commons.io;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import java.io.File;
public class FileMonitor {
public static void main(String[] args) throws Exception {
File folder = FileUtils.getTempDirectory();
startFileMonitor(folder);
}
/**
* @param folder
* @throws Exception
*/
public static void startFileMonitor(File folder) throws Exception {
FileAlterationObserver observer = new FileAlterationObserver(folder);
FileAlterationMonitor monitor = new FileAlterationMonitor(5000);
FileAlterationListener fal = new FileAlterationListenerAdaptor() {
@Override
public void onFileCreate(File file) {
// on create action
}
@Override
public void onFileDelete(File file) {
// on delete action
}
};
observer.addListener(fal);
monitor.addObserver(observer);
monitor.start();
}
}

View File

@@ -0,0 +1,123 @@
package com.baeldung.commons.io;
import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.comparator.PathFileComparator;
import org.apache.commons.io.comparator.SizeFileComparator;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.NameFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.io.input.TeeInputStream;
import org.apache.commons.io.output.TeeOutputStream;
import org.junit.Assert;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
public class CommonsIOUnitTest {
@Test
public void whenCopyANDReadFileTesttxt_thenMatchExpectedData() throws IOException {
String expectedData = "Hello World from fileTest.txt!!!";
File file = FileUtils.getFile(getClass().getClassLoader().getResource("fileTest.txt").getPath());
File tempDir = FileUtils.getTempDirectory();
FileUtils.copyFileToDirectory(file, tempDir);
File newTempFile = FileUtils.getFile(tempDir, file.getName());
String data = FileUtils.readFileToString(newTempFile, Charset.defaultCharset());
Assert.assertEquals(expectedData, data.trim());
}
@Test
public void whenUsingFileNameUtils_thenshowdifferentFileOperations() throws IOException {
String path = getClass().getClassLoader().getResource("fileTest.txt").getPath();
String fullPath = FilenameUtils.getFullPath(path);
String extension = FilenameUtils.getExtension(path);
String baseName = FilenameUtils.getBaseName(path);
System.out.println("full path" + fullPath);
System.out.println("Extension" + extension);
System.out.println("Base name" + baseName);
}
@Test
public void whenUsingFileSystemUtils_thenDriveFreeSpace() throws IOException {
long freeSpace = FileSystemUtils.freeSpaceKb("/");
}
@SuppressWarnings("resource")
@Test
public void whenUsingTeeInputOutputStream_thenWriteto2OutputStreams() throws IOException {
final String str = "Hello World.";
ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
FilterOutputStream teeOutputStream = new TeeOutputStream(outputStream1, outputStream2);
new TeeInputStream(inputStream, teeOutputStream, true).read(new byte[str.length()]);
Assert.assertEquals(str, String.valueOf(outputStream1));
Assert.assertEquals(str, String.valueOf(outputStream2));
}
@Test
public void whenGetFilewithNameFileFilter_thenFindfileTesttxt() throws IOException {
final String testFile = "fileTest.txt";
String path = getClass().getClassLoader().getResource(testFile).getPath();
File dir = FileUtils.getFile(FilenameUtils.getFullPath(path));
String[] possibleNames = { "NotThisOne", testFile };
Assert.assertEquals(testFile, dir.list(new NameFileFilter(possibleNames, IOCase.INSENSITIVE))[0]);
}
@Test
public void whenGetFilewith_ANDFileFilter_thenFindsampletxt() throws IOException {
String path = getClass().getClassLoader().getResource("fileTest.txt").getPath();
File dir = FileUtils.getFile(FilenameUtils.getFullPath(path));
Assert.assertEquals("sample.txt", dir.list(new AndFileFilter(new WildcardFileFilter("*ple*", IOCase.INSENSITIVE), new SuffixFileFilter("txt")))[0]);
}
@Test
public void whenSortDirWithPathFileComparator_thenFirstFileaaatxt() throws IOException {
PathFileComparator pathFileComparator = new PathFileComparator(IOCase.INSENSITIVE);
String path = FilenameUtils.getFullPath(getClass().getClassLoader().getResource("fileTest.txt").getPath());
File dir = new File(path);
File[] files = dir.listFiles();
pathFileComparator.sort(files);
Assert.assertEquals("aaa.txt", files[0].getName());
}
@Test
public void whenSizeFileComparator_thenLargerFile() throws IOException {
SizeFileComparator sizeFileComparator = new SizeFileComparator();
File largerFile = FileUtils.getFile(getClass().getClassLoader().getResource("fileTest.txt").getPath());
File smallerFile = FileUtils.getFile(getClass().getClassLoader().getResource("sample.txt").getPath());
int i = sizeFileComparator.compare(largerFile, smallerFile);
Assert.assertTrue(i > 0);
}
}

View File

@@ -0,0 +1,55 @@
package com.baeldung.commons.io.csv;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.junit.Test;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class CSVReaderWriterUnitTest {
public static final Map<String, String> AUTHOR_BOOK_MAP = Collections.unmodifiableMap(new LinkedHashMap<String, String>() {
{
put("Dan Simmons", "Hyperion");
put("Douglas Adams", "The Hitchhiker's Guide to the Galaxy");
}
});
public static final String[] HEADERS = { "author", "title" };
public static final String EXPECTED_FILESTREAM = "author,title\r\n" + "Dan Simmons,Hyperion\r\n" + "Douglas Adams,The Hitchhiker's Guide to the Galaxy";
@Test
public void givenCSVFile_whenRead_thenContentsAsExpected() throws IOException {
Reader in = new FileReader("src/test/resources/book.csv");
Iterable<CSVRecord> records = CSVFormat.DEFAULT.withHeader(HEADERS).withFirstRecordAsHeader().parse(in);
for (CSVRecord record : records) {
String author = record.get("author");
String title = record.get("title");
assertEquals(AUTHOR_BOOK_MAP.get(author), title);
}
}
@Test
public void givenAuthorBookMap_whenWrittenToStream_thenOutputStreamAsExpected() throws IOException {
StringWriter sw = new StringWriter();
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withHeader(HEADERS))) {
AUTHOR_BOOK_MAP.forEach((author, title) -> {
try {
printer.printRecord(author, title);
} catch (IOException e) {
e.printStackTrace();
}
});
}
assertEquals(EXPECTED_FILESTREAM, sw.toString().trim());
}
}

View File

@@ -0,0 +1 @@
Hello World from aaa.txt!!!

View File

@@ -0,0 +1,3 @@
author,title
Dan Simmons,Hyperion
Douglas Adams,The Hitchhiker's Guide to the Galaxy
1 author title
2 Dan Simmons Hyperion
3 Douglas Adams The Hitchhiker's Guide to the Galaxy

View File

@@ -0,0 +1 @@
Hello World from fileTest.txt!!!

View File

@@ -0,0 +1,2 @@
line 1
a second line