first commit

This commit is contained in:
azhwani 2020-09-21 11:16:03 +01:00
parent 10e7aaed73
commit 5d29ae5bbd
2 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,61 @@
package com.baeldung.lastmodifiedfile;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Optional;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.io.filefilter.FileFilterUtils;
public class LastModifiedFileApp {
public static File findUsingIOApi(String sdir) {
File dir = new File(sdir);
if (dir.isDirectory()) {
Optional<File> opFile = Arrays.stream(dir.listFiles(File::isFile))
.max((f1, f2) -> Long.compare(f1.lastModified(), f2.lastModified()));
if (opFile.isPresent()) {
return opFile.get();
}
}
return null;
}
public static Path findUsingNIOApi(String sdir) throws IOException {
Path dir = Paths.get(sdir);
if (Files.isDirectory(dir)) {
Optional<Path> opPath = Files.list(dir)
.filter(p -> !Files.isDirectory(p))
.sorted((p1, p2) -> Long.valueOf(p2.toFile().lastModified())
.compareTo(p1.toFile().lastModified()))
.findFirst();
if (opPath.isPresent()) {
return opPath.get();
}
}
return null;
}
public static File findUsingCommonsIO(String sdir) {
File dir = new File(sdir);
if (dir.isDirectory()) {
File[] dirFiles = dir.listFiles((FileFilter) FileFilterUtils.fileFileFilter());
if (dirFiles != null && dirFiles.length > 0) {
Arrays.sort(dirFiles, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
return dirFiles[0];
}
}
return null;
}
}

View File

@ -0,0 +1,72 @@
package com.baeldung.lastmodifiedfile;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
public class LastModifiedFileAppUnitTest {
private final static String SOURCEDIRECTORY = "src/test/resources/lastmodfiles";
@BeforeAll
public static void setUpFiles() throws IOException, InterruptedException {
File srcDir = new File(SOURCEDIRECTORY);
if (!srcDir.exists()) {
srcDir.mkdir();
}
FileUtils.cleanDirectory(srcDir);
File file01 = new File(SOURCEDIRECTORY + "/file01.txt");
file01.createNewFile();
Thread.sleep(2000);
File file02 = new File(SOURCEDIRECTORY + "/file02.txt");
file02.createNewFile();
Thread.sleep(2000);
File file03 = new File(SOURCEDIRECTORY + "/file03.txt");
file03.createNewFile();
Thread.sleep(2000);
Files.write(Paths.get(SOURCEDIRECTORY + "/file02.txt"), "Hello File02".getBytes());
}
@Test
public void givenDirectory_whenUsingIoApi_thenFindLastModfile() throws IOException {
File lastModFile = LastModifiedFileApp.findUsingIOApi(SOURCEDIRECTORY);
assertThat(lastModFile).isNotNull();
assertThat(lastModFile.getName()).isEqualTo("file02.txt");
}
@Test
public void givenDirectory_whenUsingNioApi_thenFindLastModfile() throws IOException {
Path lastModPath = LastModifiedFileApp.findUsingNIOApi(SOURCEDIRECTORY);
assertThat(lastModPath).isNotNull();
assertThat(lastModPath.toFile()
.getName()).isEqualTo("file02.txt");
}
@Test
public void givenDirectory_whenUsingApacheCommons_thenFindLastModfile() throws IOException {
File lastModFile = LastModifiedFileApp.findUsingCommonsIO(SOURCEDIRECTORY);
assertThat(lastModFile).isNotNull();
assertThat(lastModFile.getName()).isEqualTo("file02.txt");
}
}