Makes a new utility class containing methods related to file reading and filen names

This commit is contained in:
Kristian Knarvik 2020-05-08 18:54:35 +02:00
parent c6f57835f5
commit ef426f74ac

View File

@ -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 <p>The path the file should ideally be saved at.</p>
* @param extension <p>The extension of the target file.</p>
* @return <p>A filename guaranteed not to collide with other files.</p>
*/
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 <p>A file object.</p>
* @return <p>A filename.</p>
*/
public static String stripExtension(File file) {
return file.getName().substring(0, file.getName().lastIndexOf('.'));
}
/**
* Removes the extension from a file name
* @param file <p>A filename.</p>
* @return <p>A filename without its extension.</p>
*/
static String stripExtension(String file) {
return file.substring(0, file.lastIndexOf('.'));
}
/**
* Recursively lists all files in a folder
* @param folder <p>The folder to start from.</p>
* @param maxRecursions <p>Maximum number of recursions</p>
* @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
*
* <p>The file must contain the number of lines to read in the first line.</p>
*
* @param fileName <p>The file to read.</p>
* @return <p>A string list where each element is one line of the file.</p>
* @throws IOException <p>If the file cannot be read.</p>
*/
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 <p>The name of the resource you want to read.</p>
* @return <p>An input stream which can be used to access the resource.</p>
*/
private static InputStream getResourceAsStream(String resourceName) {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
return classloader.getResourceAsStream(resourceName);
}
}