In order to save files for investigating failures from within TestNG it’s important to have a safe filename that is unique to the test - otherwise you may overwrite important files. I have written the following simple method in Java that is called from a listener with an ITestResult parameter to construct a unique file name that should be safe on most file systems.

private static String fileNameFrom(ITestResult result, String fileExtension) {
  List<String> parts = new ArrayList<String>();

  //add parameters
  Object[] parameters = result.getParameters();
  for (Object parameter: parameters)
    parts.add(parameter != null ? parameter.toString() : NULL_PARAMETER_VALUE);</p>

  String fileName = StringUtils.join(parts.toArray(new String[parts.size()]), ".");
  fileName = fileName.replaceAll("[\\^\\\\.\\-:;#_]", "_");
  return fileName + "." + fileExtension;