From 110ad8f7746aca49a91fe4048e2b0f4329944a74 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Tue, 1 Oct 2024 22:37:57 -0700 Subject: code cleanup and refactor --- src/main/java/FileUtility.java | 106 +++++++++++------------------------------ 1 file changed, 28 insertions(+), 78 deletions(-) (limited to 'src/main/java/FileUtility.java') diff --git a/src/main/java/FileUtility.java b/src/main/java/FileUtility.java index 61eba87..c29d21b 100644 --- a/src/main/java/FileUtility.java +++ b/src/main/java/FileUtility.java @@ -8,19 +8,20 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; -import java.util.HashMap; +import java.util.Objects; public class FileUtility { - public void deleteFile(String fileName) { + public static void deleteFile(String fileName) { File file = new File(fileName); if (file.exists()) { - file.delete(); + if(!file.delete()) + System.out.println("Failed to delete file: " + fileName); } } - public void deleteALlFileOfType(String path, String fileExt){ + public static void deleteALlFileOfType(String path, String fileExt){ File folder = new File(path); File[] listOfFiles = folder.listFiles(); for(int i=0;i blacklist = arrayListToHashMap(readTextFile(filename),":"); - for(String key : blacklist.keySet()){ - if(s.contains(key)){ - s = s.replace(key,blacklist.get(key)); - } - } - return s; - - - } - //read a text file and return the contents as a hashmap with key value pairs - public ArrayList readTextFile(String fileName) { - ArrayList lines = new ArrayList(); - try { - BufferedReader br = new BufferedReader(new FileReader(fileName)); - String line; - while ((line = br.readLine()) != null) { - lines.add(line); - } - br.close(); - } catch (Exception e) { - e.printStackTrace(); - } - return lines; - } - - public HashMap arrayListToHashMap(ArrayList list, String delimiter) { - HashMap map = new HashMap(); - for (String line : list) { - String[] parts = line.split(delimiter); - if (parts.length >= 2) { - String key = parts[0]; - String value = parts[1]; - map.put(key, value); - } - else if(parts.length==1){ - String key = parts[0]; - String value = ""; - map.put(key, value); - } - else { - System.out.println("ignoring line: " + line); - } - } - return map; - } - - public void moveFile(String source, String destination) { - File sourceFile = new File(source); - File destinationFile = new File(destination); - sourceFile.renameTo(destinationFile); - System.out.println("Moved file to Completed Folder"); - } - - public String removeNonAlphaNumeric(String str) { - return str.replaceAll("[^a-zA-Z0-9]", ""); - } - public static File findFileWithType(String directory, String fileExt){ System.out.println("Searching for file with extension: " + fileExt + " in directory: " + directory); File dir = new File(directory); File[] files = dir.listFiles(); for(File file : files){ if(file.getName().endsWith(fileExt)){ + System.out.println("Found file: " + file.getName()); return file; } } + System.out.println("No file found with extension: " + fileExt); return null; } public static String findJsonFile(String folderName) { File folder = new File(folderName); File[] listOfFiles = folder.listFiles(); - for (int i = 0; i < listOfFiles.length; i++) { + for (int i = 0; i < Objects.requireNonNull(listOfFiles).length; i++) { if (listOfFiles[i].isFile()) { if (listOfFiles[i].getName().endsWith(".json")) { return listOfFiles[i].getAbsolutePath(); @@ -173,15 +117,6 @@ public class FileUtility { } return json; } - public static void deleteAllFilesDir(String path) { - File folder = new File(path); - File[] files = folder.listFiles(); - if (files != null) { - for (File f : files) { - f.delete(); - } - } - } public static String showImageFileChooser() { javax.swing.JFileChooser chooser = new javax.swing.JFileChooser(); @@ -197,9 +132,7 @@ public class FileUtility { } } - - - public String showDirectoryChooser(){ + public static String showDirectoryChooser(){ try { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); @@ -213,8 +146,25 @@ public class FileUtility { } return ""; } + + public static ArrayList txtToList(String fileName) { + ArrayList lines = new ArrayList(); + try { + FileReader fr = new FileReader(fileName); + BufferedReader br = new BufferedReader(fr); + String line; + while ((line = br.readLine()) != null) { + lines.add(line); + } + br.close(); + } catch (Exception e) { + e.printStackTrace(); + } + return lines; + } + //get the path of all mp3 files in a directory and return them as a file arraylist - public ArrayList getMp3FilesAsList(String path){ + public static ArrayList getMp3FilesAsList(String path){ ArrayList mp3Files = new ArrayList(); File folder = new File(path); File[] listOfFiles = folder.listFiles(); -- cgit v1.2.3