public class FileIOUtils { /** * Write JsonObject to file * @param filepath String filepath * @param contents JsonObject contents */ public static void writeFile(String filepath, JsonObject contents) { writeFile(filepath, JsonUtils.formatJsonObjectToString(contents)); } /** * Write stuff to a file * @param filepath the file * @param contents what to write into the file */ public static void writeFile(String filepath, String contents) { try { FileWriter fw = new FileWriter(filepath); fw.write(contents); fw.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Read content of a file * @param filepath the file to read from * @return the files contents */ public static String readFile(String filepath) { StringBuilder result = new StringBuilder(); try { Scanner scanner = new Scanner(new java.io.File(filepath)); while (scanner.hasNextLine()) { result.append(scanner.nextLine()); } scanner.close(); } catch (Exception e) { e.printStackTrace(); } return result.toString(); } }