BAEL-973 Apache Commons Commons IO (#2262)

* BAEL-973 Apache Commons Commons IO

* BAEL-973 : fix up formatting

* BAEL-973 : review comments.

* BAEL-973 : review comments.

* BAEL-973 : review comments.

* BAEL-973 : review comments.

* BAEL-973 : review comments.

* BAEL-973 : review comments.
This commit is contained in:
Charith De Silva 2017-09-07 13:52:48 +10:00 committed by KevinGilmore
parent 9f10f7c0f4
commit efbac53ec9
6 changed files with 199 additions and 0 deletions

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,151 @@
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 @@
Hello World from ABC.txt!!!

View File

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

View File

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

View File

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