diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/README.md b/README.md new file mode 100644 index 0000000..ec3f91f --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +## File Utils ## + +A collection of useful file utilities. + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..029d037 --- /dev/null +++ b/pom.xml @@ -0,0 +1,33 @@ + + 4.0.0 + + io.ljunggren + file-utils + 0.0.1-SNAPSHOT + jar + + file-utils + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + commons-io + commons-io + 2.8.0 + + + junit + junit + 4.13.2 + test + + + diff --git a/src/main/java/io/ljunggren/fileUtils/FileUtils.java b/src/main/java/io/ljunggren/fileUtils/FileUtils.java new file mode 100644 index 0000000..321b98b --- /dev/null +++ b/src/main/java/io/ljunggren/fileUtils/FileUtils.java @@ -0,0 +1,237 @@ +package io.ljunggren.fileUtils; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +public class FileUtils { + + public static final String NEW_LINE = System.lineSeparator(); + + public static boolean exists(File file) { + return file == null ? false : file.exists(); + } + + public static boolean exists(String path) { + return path == null ? false : exists(Paths.get(path)); + } + + public static boolean exists(Path path) { + return path == null ? false : Files.exists(path); + } + + public static String readFile(File file) throws IOException { + return readFile(file == null ? null : file.getAbsolutePath()); + } + + public static String readFile(String path) throws IOException { + return readFile(path == null ? null : Paths.get(path)); + } + + public static String readFile(Path path) throws IOException { + if (path == null) { + throw new IOException("Path is null"); + } + return new String(Files.readAllBytes(path)); + } + + public static List parseFile(File file) throws IOException { + return parseFile(file == null ? null : file.getAbsolutePath()); + } + + public static List parseFile(String path) throws IOException { + return parseFile(path == null ? null : Paths.get(path)); + } + + public static List parseFile(Path path) throws IOException { + if (path == null) { + throw new IOException("Path is null"); + } + return Files.readAllLines(path); + } + + public static String createFile(String path) throws IOException { + return createFile(path == null ? null : Paths.get(path)).toString(); + } + + public static Path createFile(Path path) throws IOException { + return createFile(path, null); + } + + public static String createFile(String path, String content) throws IOException { + return createFile(path == null ? null : Paths.get(path), content).toString(); + } + + public static Path createFile(Path path, String content) throws IOException { + if (path == null) { + throw new IOException("Path is null"); + } + byte[] bytes = content == null ? new byte[] {} : content.getBytes(); + return Files.write(path, bytes, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); + } + + public static String writeToFile(String path, String content) throws IOException { + return writeToFile(path == null ? null : Paths.get(path), content).toString(); + } + + public static Path writeToFile(Path path, String content) throws IOException { + if (path == null) { + throw new IOException("Path is null"); + } + byte[] bytes = content == null ? new byte[] {} : content.getBytes(); + return Files.write(path, bytes, StandardOpenOption.WRITE); + } + + public static String writeToFile(String path, List contents) throws IOException { + return writeToFile(path == null ? null : Paths.get(path), contents).toString(); + } + + public static Path writeToFile(Path path, List contents) throws IOException { + return writeToFile(path, contents == null ? null : String.join(NEW_LINE, contents)); + } + + public static String appendToFile(String path, String content, boolean newLine) throws IOException { + return appendToFile(path == null ? null : Paths.get(path), content, newLine).toString(); + } + + public static Path appendToFile(Path path, String content, boolean newLine) throws IOException { + if (path == null) { + throw new IOException("Path is null"); + } + if (content == null) { + return Files.write(path, new byte[] {}, StandardOpenOption.APPEND); + } + content = newLine ? NEW_LINE + content : content; + return Files.write(path, content.getBytes(), StandardOpenOption.APPEND); + } + + public static String appendToFile(String path, List contents, boolean newLine) throws IOException { + return appendToFile(path == null ? null : Paths.get(path), contents, newLine).toString(); + } + + public static Path appendToFile(Path path, List contents, boolean newLine) throws IOException { + return appendToFile(path, contents == null ? null : String.join(System.lineSeparator(), contents), newLine); + } + + public static String renameFile(String path, String newName) throws IOException { + return renameFile(path == null ? null : Paths.get(path), newName).toString(); + } + + public static Path renameFile(Path path, String newName) throws IOException { + if (path == null) { + throw new IOException("Path is null"); + } + return Files.move(path, path.resolveSibling(newName), StandardCopyOption.REPLACE_EXISTING); + } + + public static String moveFile(String sourcePath, String targetPath) throws IOException { + return moveFile(sourcePath == null ? null : Paths.get(sourcePath), + targetPath == null ? null : Paths.get(targetPath)).toString(); + } + + public static Path moveFile(Path sourcePath, Path targetPath) throws IOException { + if (sourcePath == null) { + throw new IOException("Source path is null"); + } + if (targetPath == null) { + throw new IOException("Target path is null"); + } + return Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); + } + + public static String copyFile(String sourcePath, String targetPath) throws IOException { + return copyFile(sourcePath == null ? null : Paths.get(sourcePath), + targetPath == null ? null : Paths.get(targetPath)).toString(); + } + + public static Path copyFile(Path sourcePath, Path targetPath) throws IOException { + if (sourcePath == null) { + throw new IOException("Source path is null"); + } + if (targetPath == null) { + throw new IOException("Target path is null"); + } + return Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); + } + + public static String truncateFile(String path) throws IOException { + return truncateFile(path == null ? null : Paths.get(path)).toString(); + } + + public static Path truncateFile(Path path) throws IOException { + if (path == null) { + throw new IOException("Path is null"); + } + return Files.write(path, new byte[0], StandardOpenOption.TRUNCATE_EXISTING); + } + + public static void deleteFile(String path) throws IOException { + deleteFile(path == null ? null : Paths.get(path)); + } + + public static void deleteFile(Path path) throws IOException { + if (path == null) { + throw new IOException("Path is null"); + } + Files.delete(path); + } + + public static List listFiles(String path) throws IOException { + if (path == null ) { + throw new IOException("Path is null"); + } + return Arrays.stream(new File(path).listFiles()) + .filter(file -> file.isFile()) + .collect(Collectors.toList()); + } + + public static List listFiles(Path path) throws IOException { + return listFiles(path == null ? null : path.toString()); + } + + public static List filterByPrefix(List files, String prefix) { + if (files == null || prefix == null) { + return new ArrayList(); + } + return files.stream() + .filter(file -> file.getName().startsWith(prefix)) + .collect(Collectors.toList()); + } + + public static List filterBySuffix(List files, String suffix) { + if (files == null || suffix == null) { + return new ArrayList(); + } + return files.stream() + .filter(file -> file.getName().endsWith(suffix)) + .collect(Collectors.toList()); + } + + public static void sortByLastModifiedDate(List files) { + sortByLastModifiedDate(files, true); + } + + public static void sortByLastModifiedDate(List files, boolean ascending) { + if (files == null) { + return; + } + Collections.sort(files, new Comparator() { + public int compare(File f1, File f2) { + return ascending ? + ((Long) f1.lastModified()).compareTo((Long) f2.lastModified()) : + ((Long) f2.lastModified()).compareTo((Long) f1.lastModified()); + } + }); + } + +} diff --git a/src/test/java/io/ljunggren/fileUtils/FileUtilsTest.java b/src/test/java/io/ljunggren/fileUtils/FileUtilsTest.java new file mode 100644 index 0000000..2f0f363 --- /dev/null +++ b/src/test/java/io/ljunggren/fileUtils/FileUtilsTest.java @@ -0,0 +1,470 @@ +package io.ljunggren.fileUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class FileUtilsTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void instantiationTest() { + assertNotNull(new FileUtils()); + } + + @Test + public void existsTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + assertTrue(FileUtils.exists(file)); + } + + @Test + public void existsNullFileTest() { + File file = null; + assertFalse(FileUtils.exists(file)); + } + + @Test + public void existsNullStringTest() { + String path = null; + assertFalse(FileUtils.exists(path)); + } + + @Test + public void existsNullPathTest() { + Path path = null; + assertFalse(FileUtils.exists(path)); + } + + @Test + public void existsStringPathTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + assertTrue(FileUtils.exists(file.getAbsolutePath())); + } + + @Test + public void existsStringPathFalseTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + FileUtils.deleteFile(path); + assertFalse(FileUtils.exists(path)); + } + + @Test + public void readFileTest() throws IOException { + String content = "This is a test file."; + File file = temporaryFolder.newFile("test.txt"); + FileUtils.writeToFile(file.getAbsolutePath(), content); + String readContent = FileUtils.readFile(file); + assertEquals(content, readContent); + } + + @Test(expected = IOException.class) + public void readFileNullTest() throws IOException { + File file = null; + FileUtils.readFile(file); + } + + @Test + public void parseFileTest() throws IOException { + List content = Arrays.asList(new String[] { + "This is a test file", + "with two lines." + }); + File file = temporaryFolder.newFile("test.txt"); + FileUtils.writeToFile(file.getAbsolutePath(), content); + List readContent = FileUtils.parseFile(file); + assertEquals(content, readContent); + } + + @Test(expected = IOException.class) + public void parseFileNullTest() throws IOException { + File file = null; + FileUtils.parseFile(file); + } + + @Test + public void createFileTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + FileUtils.createFile(path); + assertTrue(FileUtils.exists(path)); + } + + @Test + public void createFileWithContentTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + String content = "This is a test file."; + FileUtils.createFile(path, content); + assertTrue(FileUtils.exists(path)); + assertEquals(content, FileUtils.readFile(path)); + } + + @Test(expected = IOException.class) + public void createFileNullTest() throws IOException { + String path = null; + FileUtils.createFile(path); + } + + @Test + public void writeToFileTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + String content = "This is a test file."; + FileUtils.createFile(path); + FileUtils.writeToFile(path, content); + assertEquals(content, FileUtils.readFile(path)); + } + + @Test + public void writeToFileNullContentTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + FileUtils.createFile(path); + String content = null; + FileUtils.writeToFile(path, content); + assertTrue(FileUtils.readFile(path).isEmpty()); + } + + @Test(expected = IOException.class) + public void writeToFileNullPathTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + FileUtils.createFile(path); + path = null; + FileUtils.writeToFile(path, "This is a test file"); + } + + @Test + public void writeToFileListTest() throws IOException { + List contents = Arrays.asList(new String[] { + "This is a test file", + "with two lines." + }); + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + FileUtils.writeToFile(path, contents); + String readContent = FileUtils.readFile(path); + assertEquals(String.join(FileUtils.NEW_LINE, contents), readContent); + } + + @Test + public void writeToFileListNullListTest() throws IOException { + List contents = null; + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + FileUtils.writeToFile(path, contents); + assertTrue(FileUtils.readFile(path).isEmpty()); + } + + @Test(expected = IOException.class) + public void writeToFileListNullPathTest() throws IOException { + List contents = Arrays.asList(new String[] { + "This is a test file", + "with two lines." + }); + String path = null; + FileUtils.writeToFile(path, contents); + } + + @Test + public void appendToFileTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + String content = "This is a test file."; + String appendedContent = "More text"; + FileUtils.createFile(path, content); + FileUtils.appendToFile(path, appendedContent, false); + assertEquals(content + appendedContent, FileUtils.readFile(path)); + } + + @Test + public void appendToFileNewLineTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + String content = "This is a test file."; + String appendedContent = "More text"; + FileUtils.createFile(path, content); + FileUtils.appendToFile(path, appendedContent, true); + assertEquals(content + FileUtils.NEW_LINE + appendedContent, FileUtils.readFile(path)); + } + + @Test + public void appendToFileNullContentTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + String content = "This is a test file."; + String appendedContent = null; + FileUtils.createFile(path, content); + FileUtils.appendToFile(path, appendedContent, true); + assertEquals(content, FileUtils.readFile(path)); + } + + @Test(expected = IOException.class) + public void appendToFileNullPathTest() throws IOException { + String path = null; + String content = "This is a test file."; + String appendedContent = "More text"; + FileUtils.createFile(path, content); + FileUtils.appendToFile(path, appendedContent, true); + } + + @Test + public void appendToFileListTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + String content = "This is a test file."; + List appendedContents = Arrays.asList(new String[] { + "This is a test file", + "with two lines." + }); + FileUtils.createFile(path, content); + FileUtils.appendToFile(path, appendedContents, false); + assertEquals(content + String.join(FileUtils.NEW_LINE, appendedContents), FileUtils.readFile(path)); + } + + @Test + public void appendToFileListNewLineTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + String content = "This is a test file."; + List appendedContents = Arrays.asList(new String[] { + "This is a test file", + "with two lines." + }); + FileUtils.createFile(path, content); + FileUtils.appendToFile(path, appendedContents, true); + assertEquals(content + FileUtils.NEW_LINE + String.join(FileUtils.NEW_LINE, appendedContents), FileUtils.readFile(path)); + } + + @Test + public void appendToFileListNullContentTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + String content = "This is a test file."; + List appendedContents = null; + FileUtils.createFile(path, content); + FileUtils.appendToFile(path, appendedContents, true); + assertEquals(content, FileUtils.readFile(path)); + } + + @Test(expected = IOException.class) + public void appendToFileListNullPathTest() throws IOException { + String path = null; + String content = "This is a test file."; + List appendedContents = Arrays.asList(new String[] { + "This is a test file", + "with two lines." + }); + FileUtils.createFile(path, content); + FileUtils.appendToFile(path, appendedContents, true); + } + + @Test + public void renameFileTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + String newPath = FileUtils.renameFile(path, "test2.txt"); + assertTrue(FileUtils.exists(newPath)); + } + + @Test(expected = IOException.class) + public void renameFileNullPathTest() throws IOException { + String path = null; + FileUtils.renameFile(path, "test2.txt"); + } + + @Test + public void moveFileTest() throws IOException { + File sourceFile = temporaryFolder.newFile("test.txt"); + String sourcePath = sourceFile.getAbsolutePath(); + String targetPath = sourceFile.getParentFile().getAbsolutePath() + "test2.txt"; + FileUtils.moveFile(sourcePath, targetPath); + assertTrue(FileUtils.exists(targetPath)); + } + + @Test(expected = IOException.class) + public void moveFileSourcePathNullTest() throws IOException { + File sourceFile = temporaryFolder.newFile("test.txt"); + String sourcePath = null; + String targetPath = sourceFile.getParentFile().getAbsolutePath() + "test2.txt"; + FileUtils.moveFile(sourcePath, targetPath); + } + + @Test(expected = IOException.class) + public void moveFileTargetPathNullTest() throws IOException { + File sourceFile = temporaryFolder.newFile("test.txt"); + String sourcePath = sourceFile.getAbsolutePath(); + String targetPath = null; + FileUtils.moveFile(sourcePath, targetPath); + } + + @Test + public void copyFileTest() throws IOException { + File sourceFile = temporaryFolder.newFile("test.txt"); + String sourcePath = sourceFile.getAbsolutePath(); + String targetPath = sourceFile.getParentFile().getAbsolutePath() + "test2.txt"; + FileUtils.copyFile(sourcePath, targetPath); + assertEquals(FileUtils.readFile(sourcePath), FileUtils.readFile(targetPath)); + } + + @Test(expected = IOException.class) + public void copyFileSourcePathNullTest() throws IOException { + File sourceFile = temporaryFolder.newFile("test.txt"); + String sourcePath = null; + String targetPath = sourceFile.getParentFile().getAbsolutePath() + "test2.txt"; + FileUtils.copyFile(sourcePath, targetPath); + } + + @Test(expected = IOException.class) + public void copyFileTargetPathNullTest() throws IOException { + File sourceFile = temporaryFolder.newFile("test.txt"); + String sourcePath = sourceFile.getAbsolutePath(); + String targetPath = null; + FileUtils.copyFile(sourcePath, targetPath); + } + + @Test + public void truncateTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + String content = "This is a test file."; + FileUtils.createFile(path, content); + FileUtils.truncateFile(path); + assertEquals("", FileUtils.readFile(path)); + } + + @Test(expected = IOException.class) + public void truncateNullPathTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + String content = "This is a test file."; + FileUtils.createFile(path, content); + path = null; + FileUtils.truncateFile(path); + } + + @Test + public void deleteFileTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + FileUtils.createFile(path); + FileUtils.deleteFile(path); + assertFalse(FileUtils.exists(path)); + } + + @Test(expected = IOException.class) + public void deleteFileNullPathTest() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + String path = file.getAbsolutePath(); + FileUtils.createFile(path); + path = null; + FileUtils.deleteFile(path); + } + + @Test + public void listFilesTest() throws IOException { + List files = Arrays.asList(new File[] { + temporaryFolder.newFile("test.txt"), + temporaryFolder.newFile("test.pdf"), + temporaryFolder.newFile("test.doc") + }); + String path = temporaryFolder.getRoot().getAbsolutePath(); + List readFiles = FileUtils.listFiles(Paths.get(path)); + Collections.sort(files); + Collections.sort(readFiles); + assertEquals(files, readFiles); + } + + @Test(expected = IOException.class) + public void listFilesNullPathTest() throws IOException { + Path path = null; + FileUtils.listFiles(path); + } + + @Test + public void filterByPrefixTest() throws IOException { + List files = Arrays.asList(new File[] { + temporaryFolder.newFile("test.txt"), + temporaryFolder.newFile("test.pdf"), + temporaryFolder.newFile("info.doc") + }); + List filteredFiles = FileUtils.filterByPrefix(files, "test"); + assertTrue(filteredFiles.size() == 2); + } + + @Test + public void filterByPrefixEmptyListTest() { + List filteredFiles = FileUtils.filterByPrefix(new ArrayList(), "test"); + assertTrue(filteredFiles.isEmpty()); + } + + @Test + public void filterByPrefixNullListTest() { + List filteredFiles = FileUtils.filterByPrefix(null, "test"); + assertTrue(filteredFiles.isEmpty()); + } + + @Test + public void filterByPrefixNullPrefixTest() throws IOException { + List files = Arrays.asList(new File[] { + temporaryFolder.newFile("test.txt"), + temporaryFolder.newFile("test.pdf"), + temporaryFolder.newFile("info.doc") + }); + List filteredFiles = FileUtils.filterByPrefix(files, null); + assertTrue(filteredFiles.size() == 0); + } + + @Test + public void filterBySuffixTest() throws IOException { + List files = Arrays.asList(new File[] { + temporaryFolder.newFile("test.txt"), + temporaryFolder.newFile("test.pdf"), + temporaryFolder.newFile("info.txt") + }); + List filteredFiles = FileUtils.filterBySuffix(files, "txt"); + assertTrue(filteredFiles.size() == 2); + } + + @Test + public void filterBySuffixEmptyListTest() { + List filteredFiles = FileUtils.filterBySuffix(new ArrayList(), "txt"); + assertTrue(filteredFiles.isEmpty()); + } + + @Test + public void filterBySuffixNullListTest() { + List filteredFiles = FileUtils.filterBySuffix(null, "txt"); + assertTrue(filteredFiles.isEmpty()); + } + + @Test + public void filterBySuffixNullSuffixTest() throws IOException { + List files = Arrays.asList(new File[] { + temporaryFolder.newFile("test.txt"), + temporaryFolder.newFile("test.pdf"), + temporaryFolder.newFile("info.txt") + }); + List filteredFiles = FileUtils.filterBySuffix(files, null); + assertTrue(filteredFiles.size() == 0); + } + +}