public String saveFile(File directory) throws IOException {
    String options = "(Y/N)";
    File file = new File(directory, filename).getAbsoluteFile();

    Gfsh gfsh = Gfsh.getCurrentInstance();
    if (file.exists()) {
      String fileExistsMessage = String.format("File \"%s\" already exists", filename);
      if (gfsh != null && !gfsh.isQuietMode()) {
        fileExistsMessage += " Overwrite? " + options + " : ";
        String interaction = gfsh.interact(fileExistsMessage);
        if (!"y".equalsIgnoreCase(interaction.trim())) {
          // do not save file & continue
          return "User aborted. Did not overwrite " + file.getAbsolutePath();
        }
      } else {
        return fileExistsMessage;
      }
    }
    if (type == FileResultModel.FILE_TYPE_TEXT) {
      FileWriter fw = new FileWriter(file);
      BufferedWriter bw = new BufferedWriter(fw);
      bw.write(new String(data));
      bw.flush();
      fw.flush();
      fw.close();
    } else if (type == FileResultModel.FILE_TYPE_BINARY) {
      FileOutputStream fos = new FileOutputStream(file);
      fos.write(data);
      fos.flush();
      fos.close();
    }
    return "File saved to " + file.getAbsolutePath();
  }
at this point, the dir should already exist and is confirmed as a directory
filename in this instance should be file name only. no path in the file name

@param directory the directory where to write the content of byte[] to with the filename
@return the message you would like to return to the user.
geode-gfsh/src/main/java/org/apache/geode/management/internal/cli/result/model/FileResultModel.java