From ef426f74ac7c8d787c3139c3c3dfa4057792f2f0 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 8 May 2020 18:54:35 +0200 Subject: [PATCH] Makes a new utility class containing methods related to file reading and filen names --- .../ffmpegconverter/utility/FileUtil.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/main/java/net/knarcraft/ffmpegconverter/utility/FileUtil.java 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 extension

The extension of the target file.

+ * @return

A 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 file

A file object.

+ * @return

A filename.

+ */ + public static String stripExtension(File file) { + return file.getName().substring(0, file.getName().lastIndexOf('.')); + } + + /** + * Removes the extension from a file name + * @param file

A filename.

+ * @return

A filename without its extension.

+ */ + static String stripExtension(String file) { + return file.substring(0, file.lastIndexOf('.')); + } + + /** + * Recursively lists all files in a folder + * @param folder

The folder to start from.

+ * @param maxRecursions

Maximum 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 fileName

The file to read.

+ * @return

A string list where each element is one line of the file.

+ * @throws IOException

If 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 resourceName

The name of the resource you want to read.

+ * @return

An 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); + } + +}