Method writeToFile automatically creates parent folder if it doesn't exist yet.