Useful Java methods and classes

Append text to file method

//Appends text to file in new line //Creates file if not exists //Returns false if exception occurs
private boolean appendTextToFile(String filePath, String stringToAppend) { File resFile = new File(filePath); if (!resFile.exists()) { try { resFile.createNewFile(); } catch (IOException ex) { System.out.println(ex.toString()); return false; } } try ( FileWriter fw = new FileWriter(resFile, true); BufferedWriter bw = new BufferedWriter(fw); PrintWriter pw = new PrintWriter(bw)){ pw.println(stringToAppend); } catch (IOException iOException) { System.out.println(iOException.toString()); return false; } return true; }