Print Generic List to File

Print out the contents of a Generic List to a text File.

Javadoc available at https://www.javatapas.com/docs/javatapas/io/PrintListToFile.html


public static <E> void printListToFile (String dirName, String fileName, List<E> list) throws IOException {

  if (!Files.exists(Paths.get(dirName))){Files.createDirectories(Paths.get(dirName));}
  OutputStream outputStream = new FileOutputStream(dirName + File.separator + fileName);
  PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  Iterator<E> it = list.iterator();
  while (it.hasNext()){printWriter.println(it.next().toString());}
  printWriter.flush();
  printWriter.close();
  outputStream.close();

}