/** * Deletes the given file * * @param file The file to be deleted * @return true or false depending on whether the file was succesfully deleted or not */ public static boolean deleteFile(File file) { if (file.exists()) { file.delete(); if (!file.exists()) { return true; } ChatterBox.error("utilities.BitStylus", "deleteFile", "The file could not be deleted."); } else { ChatterBox.error("utilities.BitStylus", "deleteFile", "The file does not exist."); } return false; }
/** * Returns a FileInputStream for the file at the given path * * @param path The path to the file that you want to open * @return A FileInputstream for the given path */ public static FileInputStream getFile(String path) { try { return new FileInputStream(path); } catch (Exception ex) { ChatterBox.error("ExoSkeleton", "getFile()", ex.getMessage()); return null; } }
/** * Takes a text file and String delimiter, reads the file, and returns an ArrayList of the split * contents. * * @param file The file to read * @param delimiter The String to split the text with * @return An ArrayList of the contents of the file */ public static ArrayList<String> splitTextFile(File file, String delimiter) { Scanner s = null; try { s = new Scanner(file).useDelimiter(delimiter); } catch (FileNotFoundException ex) { ChatterBox.error("utilities.BitStylus", "splitTextFile", "Not a valid text file"); return null; } catch (NullPointerException ex) { ChatterBox.error("utilities.BitStylus", "splitTextFile", "Not a valid text file"); return null; } ArrayList<String> temp = new ArrayList<String>(); while (s.hasNext()) { temp.add(s.next()); } return temp; }
/** * Takes a String and saves it to the given file * * @param file The file to save the String to * @param text The String to be saved to the file */ public static void saveTextToFile(File file, String text) { try { FileWriter outFile = new FileWriter(file); if (text != null) { outFile.write(text); } outFile.close(); } catch (IOException ex) { ChatterBox.error("utilities.BitStylus", "saveTextToFile", "Invalid file: " + file.getPath()); } }
/** * Takes a text file and returns the contents as a String * * @param file The file to read * @return A string of the entire contents of the file */ public static String readTextFile(File file) { Scanner s = null; try { s = new Scanner(file).useDelimiter(""); } catch (FileNotFoundException ex) { ChatterBox.error("utilities.BitStylus", "splitTextFile", "Not a valid text file"); } String text = ""; while (s.hasNext()) { text = text + s.next(); } return text; }