code: public static String getSafeFileName( String fileName ) {
          // not allowed chars in win and linux
          char[] notAllowed = new char[]{'/', '>', '<', ':', '"', '/', '\\', '|', '?', '*'};
          char escape = '_';
          int len = fileName.length();
          StringBuilder sb = new StringBuilder(len);
          for( int i = 0; i < len; i++ ) {
              char ch = fileName.charAt(i);
  
              boolean trapped = false;
              for( char forbidden : notAllowed ) {
                  if (ch == forbidden) {
                      sb.append(escape);
                      trapped = true;
                      break;
                  }
              }
  
              if (!trapped) {
                  sb.append(ch);
              }
          }
          String newName = sb.toString();
          // no space at end is allowed
          newName = newName.trim();
  
          // win doesn't allow dots at the end
          if (newName.endsWith(".")) {
              newName = newName.substring(0, newName.length() - 1);
          }
  
          // reserved words for windows
          String[] reservedWindows = {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8",
                  "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"};
  
          for( String reservedWin : reservedWindows ) {
              if (newName.equals(reservedWin)) {
                  newName = newName + "_";
                  break;
              }
          }
  
          return newName;
      }
docs: Makes a file name safe to be used.
  
  @param fileName the file name to "encode".
  @return the safe filename.