Java File Name Sanitize sanitizeFileName(String name)

Here you can find the source of sanitizeFileName(String name)

Description

Sanitizes the name as a valid fat32 filename.

License

Apache License

Parameter

Parameter Description
name name

Declaration

public static String sanitizeFileName(String name) 

Method Source Code

//package com.java2s;
/*//  w w  w. j av a 2 s  .  com
 * Copyright (C) 2014 Antew
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Sanitizes the name as a valid fat32 filename. For simplicity, fat32
     * filename characters may be any combination of letters, digits, or
     * characters with code point values greater than 127. Replaces the invalid
     * characters with "_" and collapses multiple "_" together.
     * <p/>
     * This is based on code from the MyTracks application <a href="http://code.google.com/p/mytracks/">on Google Code</a>
     *
     * @param name
     *     name
     */
    public static String sanitizeFileName(String name) {
        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("_");
            }
        }

        String result = buffer.toString().replaceAll(" ", "_").replaceAll(",", "").replaceAll("\"", "")
                .replaceAll("_+", "_");

        if (result.length() > 127) {
            result = result.substring(0, 127);
        }

        if (result.startsWith("_")) {
            result = result.substring(1);
        }

        if (result.endsWith("_")) {
            result = result.substring(0, result.length() - 1);
        }

        return result;
    }

    /**
     * Returns true if it is a special FAT32 character.
     * <p/>
     * This is from the MyTracks application <a href="http://code.google.com/p/mytracks/">on Google Code</a>
     *
     * @param character
     *     the character
     */
    private static boolean isSpecialFat32(char character) {
        switch (character) {
        case '$':
        case '%':
        case '\'':
        case '-':
        case '_':
        case '@':
        case '~':
        case '`':
        case '!':
        case '(':
        case ')':
        case '{':
        case '}':
        case '^':
        case '#':
        case '&':
        case '+':
        case ',':
        case ';':
        case '=':
        case '[':
        case ']':
        case ' ':
            return true;
        default:
            return false;
        }
    }
}

Related

  1. sanitizeFilename(String filename)
  2. sanitizeFilename(String fileName)
  3. sanitizeFileName(String filename)
  4. sanitizeFilename(String name)
  5. sanitizeFilename(String name)
  6. sanitizeFileName(String name)
  7. sanitizeFileName(String s)
  8. sanitizeFilename(String toClean)
  9. sanitizeFilenameForWindows(String filename)