Java Utililty Methods File Name Sanitize

List of utility methods to do File Name Sanitize

Description

The list of methods to do File Name Sanitize are organized into topic(s).

Method

Stringsanitize(String fileName)
sanitize
StringBuffer buffer = new StringBuffer();
for (char c : fileName.toCharArray()) {
    if (Character.isJavaIdentifierPart(c)) {
        buffer.append(c);
if (buffer.length() == 0) {
    buffer.append(System.currentTimeMillis());
...
StringsanitizeBlankNodeName(String filename)
Blank node names should start with a letter and have only letters/numbers.
StringBuffer ret = new StringBuffer(filename.length());
if (!filename.isEmpty()) {
    char charAt0 = filename.charAt(0);
    if (Character.isLetter(charAt0))
        ret.append(charAt0);
for (int i = 1; i < filename.length(); i++) {
    char ch = filename.charAt(i);
...
StringsanitizeFilename(String filename)
sanitize Filename
return filename.replaceAll("[^a-zA-Z0-9.-]", "_");
StringsanitizeFileName(String filename)
Given an input, return a sanitized form of the input suitable for use as a file/directory name
return sanitizeFileName(filename, SANITIZED_CHAR);
StringsanitizeFileName(String filename)
sanitize File Name
if (filename == null)
    return null;
if (filename.contains("/")) {
    filename = filename.replace("/", "_");
if (filename.contains(":")) {
    filename = filename.replace(":", "_");
if (filename.contains("*")) {
    filename = filename.replace("*", "_");
if (filename.contains("?")) {
    filename = filename.replace("?", "_");
if (filename.contains("\"")) {
    filename = filename.replace("\"", "_");
if (filename.contains("<")) {
    filename = filename.replace("<", "_");
if (filename.contains(">")) {
    filename = filename.replace(">", "_");
if (filename.contains("|")) {
    filename = filename.replace("|", "_");
if (filename.contains("\\")) {
    filename = filename.replace("\\", "_");
if (filename.contains("&")) 
    filename = filename.replace("&", "_");
return filename;
StringsanitizeFilename(String fileName)
sanitize Filename
if (fileName == null) {
    return null;
return fileName.replaceAll("/", ".").replaceAll("[\\[|\\]]", " ").replaceAll(" ", "");
StringsanitizeFilename(String name)
Substitute any characters to be avoided in a file name with '_'.
if (name.isEmpty())
    throw new IllegalArgumentException("Empty file name!");
return name.replaceAll("[\\W&&[^.-]]", "_");
StringsanitizeFileName(String name)
sanitize File Name
String result = "";
if (name == null)
    return result;
for (int k = 0; k < name.length(); k++) {
    char c = name.charAt(k);
    if (Character.isLetterOrDigit(c))
        result += c;
    else
...
StringsanitizeFilename(String name)
Replace forbidden characters with "_" in a filename.
return name.replaceAll("[:\\\\/*?|<>]", "_");
StringsanitizeFileName(String name)
Sanitizes the name as a valid fat32 filename.
StringBuffer buffer = new StringBuffer(name.length());
for (int i = 0; i < name.length(); i++) {
    int codePoint = name.codePointAt(i);
    char character = name.charAt(i);
    if (Character.isLetterOrDigit(character) || isSpecialFat32(character)) {
        buffer.appendCodePoint(codePoint);
    } else {
        buffer.append("_");
...