diff --git a/README.md b/README.md index 1815768..4a53f78 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,13 @@ A collection of useful file utilities. +Get File + +```java +File file = FileUtils.get(path); +File file = FileUtils.get(pathString); +``` + Check if File exists ```java @@ -10,6 +17,13 @@ boolean exists = FileUtils.exists(pathString); ``` +Create Directories + +```java +Path path = FileUtils.createDirectories(path); +Path path = FileUtils.createDirectories(pathString); +``` + Read File to string ```java @@ -118,6 +132,15 @@ ```java FileUtils.deleteFile(path); FileUtils.deleteFile(pathString); +FileUtils.deleteFile(file); +``` + +Delete Directory + +```java +FileUtils.deleteDirectory(path); +FileUtils.deleteDirectory(pathString); +FileUtils.deleteDirectory(file); ``` List Files (not including directories) @@ -179,3 +202,21 @@ ```java String extension = FileUtils.getFileExtension(file); ``` + +File Size + +```java +long size = FileUtils.getSize(file); +``` + +Human Readable Size + +```java +String humanReadableSize = FileUtils.getHumanReadableSize(file); +``` + +Input Stream to File + +```java +FileUtils.copyInputStreamToFile(source, destination); +``` diff --git a/src/main/java/io/ljunggren/file/utils/FileUtils.java b/src/main/java/io/ljunggren/file/utils/FileUtils.java index b8a38ba..13547bc 100644 --- a/src/main/java/io/ljunggren/file/utils/FileUtils.java +++ b/src/main/java/io/ljunggren/file/utils/FileUtils.java @@ -20,6 +20,14 @@ public class FileUtils { public static final String NEW_LINE = System.lineSeparator(); + + public static File get(Path path) { + return get(path.toString()); + } + + public static File get(String path) { + return new File(path); + } public static boolean exists(File file) { return file == null ? false : file.exists(); @@ -33,6 +41,14 @@ return path == null ? false : Files.exists(path); } + public static Path createDirectories(String path) throws IOException { + return createDirectories(Paths.get(path)); + } + + public static Path createDirectories(Path path) throws IOException { + return Files.createDirectories(path); + } + public static String readFile(File file) throws IOException { return readFile(file == null ? null : file.getAbsolutePath()); } @@ -196,6 +212,25 @@ } Files.delete(path); } + + public static void deleteFile(File file) throws IOException { + deleteFile(file == null ? null : file.getPath()); + } + + public static void deleteDirectory(String path) throws IOException { + if (path == null) { + throw new IOException("Path is null"); + } + org.apache.commons.io.FileUtils.deleteDirectory(new File(path)); + } + + public static void deleteDirectory(Path path) throws IOException { + deleteDirectory(path == null ? null : path.toString()); + } + + public static void deleteDirectory(File file) throws IOException { + deleteDirectory(file == null ? null : file.getPath()); + } public static List listFiles(String path) throws IOException { if (path == null ) { @@ -291,4 +326,22 @@ return filename.substring(filename.lastIndexOf(".") + 1); } + public static long getSize(File file) throws IOException { + if (file == null) { + throw new IOException("File is null"); + } + return file.length(); + } + + public static String getHumanReadableSize(File file) throws IOException { + if (file == null) { + throw new IOException("File is null"); + } + return org.apache.commons.io.FileUtils.byteCountToDisplaySize(file.length()); + } + + public static void inputStreamToFile(InputStream source, File destination) throws IOException { + org.apache.commons.io.FileUtils.copyInputStreamToFile(source, destination); + } + }