diff --git a/src/main/java/net/knarcraft/ffmpegconverter/utility/FileUtil.java b/src/main/java/net/knarcraft/ffmpegconverter/utility/FileUtil.java new file mode 100644 index 0000000..281c783 --- /dev/null +++ b/src/main/java/net/knarcraft/ffmpegconverter/utility/FileUtil.java @@ -0,0 +1,105 @@ +package net.knarcraft.ffmpegconverter.utility; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +/** + * A class which helps with file handling + */ +public final class FileUtil { + + private FileUtil() { + } + + /** + * Adds parentheses with an integer if the output file already exists + * @param targetPath
The path the file should ideally be saved at.
+ * @param extensionThe extension of the target file.
+ * @returnA filename guaranteed not to collide with other files.
+ */ + public static String getNonCollidingFilename(String targetPath, String extension) { + File newFile = new File(targetPath); + String fileName = stripExtension(targetPath); + int i = 1; + while (newFile.exists()) { + newFile = new File(fileName + "(" + i++ + ")" + "." + extension); + } + return newFile.toString(); + } + + /** + * Gets filename without extension from File object + * @param fileA file object.
+ * @returnA filename.
+ */ + public static String stripExtension(File file) { + return file.getName().substring(0, file.getName().lastIndexOf('.')); + } + + /** + * Removes the extension from a file name + * @param fileA filename.
+ * @returnA filename without its extension.
+ */ + static String stripExtension(String file) { + return file.substring(0, file.lastIndexOf('.')); + } + + /** + * Recursively lists all files in a folder + * @param folderThe folder to start from.
+ * @param maxRecursionsMaximum number of recursions
+ * @return A list of files + */ + public static File[] listFilesRecursive(File folder, String[] extensions, int maxRecursions) { + if (maxRecursions == 0) { return null; } + File[] listOfFiles = folder.listFiles((file) -> file.isFile() && + ListUtil.listContains(extensions, (item) -> file.getName().endsWith(item))); + if (listOfFiles == null) { return null; } + if (maxRecursions > 1) { + File[] listOfFolders = folder.listFiles((dir, name) -> new File(dir, name).isDirectory()); + if (listOfFolders != null) { + for (File file : listOfFolders) { + File[] nextLevel = listFilesRecursive(file, extensions, maxRecursions - 1); + if (nextLevel != null) { + listOfFiles = ListUtil.concatenate(listOfFiles, nextLevel); + } + } + } + } + return listOfFiles; + } + + /** + * Reads a file's contents to a string list + * + *The file must contain the number of lines to read in the first line.
+ * + * @param fileNameThe file to read.
+ * @returnA string list where each element is one line of the file.
+ * @throws IOExceptionIf the file cannot be read.
+ */ + public static String[] readFileLines(String fileName) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(getResourceAsStream(fileName))); + int numberOfLines = Integer.parseInt(reader.readLine()); + String[] lines = new String[numberOfLines]; + for (int i = 0; i < lines.length; i++) { + lines[i] = reader.readLine(); + } + return lines; + } + + /** + * Gets a resource as an InputStream + * @param resourceNameThe name of the resource you want to read.
+ * @returnAn input stream which can be used to access the resource.
+ */ + private static InputStream getResourceAsStream(String resourceName) { + ClassLoader classloader = Thread.currentThread().getContextClassLoader(); + return classloader.getResourceAsStream(resourceName); + } + +}