Adds a new function for deleting files recursively
This commit is contained in:
parent
3254481326
commit
8cdb1f143c
@ -16,7 +16,7 @@ import java.nio.file.StandardCopyOption;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* A holding class for methods shared between classes.
|
||||
* A holding class for methods shared between classes
|
||||
*
|
||||
* @author Kristian Knarvik <kristian.knarvik@knett.no>
|
||||
* @version 1.0.0
|
||||
@ -199,10 +199,11 @@ public final class CommonFunctions {
|
||||
StringBuilder text = new StringBuilder();
|
||||
char[] readCharacters = new char[1000];
|
||||
while (reader.ready()) {
|
||||
reader.read(readCharacters);
|
||||
if (reader.read(readCharacters) > 0) {
|
||||
text.append(readCharacters);
|
||||
readCharacters = new char[1000];
|
||||
}
|
||||
}
|
||||
return text.toString().trim();
|
||||
}
|
||||
|
||||
@ -244,4 +245,31 @@ public final class CommonFunctions {
|
||||
public static boolean nameIsValid(String name) {
|
||||
return !name.equals("") && name.matches("[^!?;,]+");
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all files within a folder
|
||||
* @param target <p>The folder to delete from</p>
|
||||
*/
|
||||
static void removeFilesRecursively(File target) {
|
||||
File[] oldFiles = target.listFiles();
|
||||
if (oldFiles == null) {
|
||||
throw new IllegalArgumentException("Unable to list files in directory");
|
||||
}
|
||||
for (File file : oldFiles) {
|
||||
if (file.isFile()) {
|
||||
if (!file.delete()) {
|
||||
throw new IllegalArgumentException("Unable to delete a file from the directory");
|
||||
}
|
||||
}
|
||||
}
|
||||
for (File file : oldFiles) {
|
||||
if (file.isDirectory()) {
|
||||
removeFilesRecursively(file);
|
||||
if (!file.delete()) {
|
||||
throw new IllegalArgumentException("Unable to delete a file from the directory");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import net.knarcraft.minecraftserverlauncher.Main;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.GUI;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.*;
|
||||
|
@ -109,12 +109,6 @@ public class JarDownloaderTest {
|
||||
if (!target.exists() && !target.mkdirs()) {
|
||||
throw new IllegalArgumentException("Unable to create the test files directory");
|
||||
}
|
||||
File[] oldFiles = target.listFiles();
|
||||
if (oldFiles == null) {
|
||||
throw new IllegalArgumentException("Unable to list files in jar test directory");
|
||||
}
|
||||
for (File file : oldFiles) {
|
||||
assertTrue(file.delete());
|
||||
}
|
||||
CommonFunctions.removeFilesRecursively(target);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user