Java Utililty Methods File Name Encode

List of utility methods to do File Name Encode

Description

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

Method

StringfileNameEncode(String fileName)
file Name Encode
if (fileName == null)
    return null;
try {
    return new String(fileName.getBytes("gbk"), "ISO-8859-1");
} catch (Exception ex) {
    return fileName;
StringfileNameEncode(String str)
This encodes a string so that it may be used as a file name, converting the illegal characters to their Unicode HEX values.
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); ++i) {
    char c = str.charAt(i);
    if (c < 32 || c > 126 || BAD_CHARS.indexOf(c) != -1) {
        sb.append("[").append(Integer.toHexString(c)).append("]");
    } else {
        sb.append(c);
return sb.toString();
StringfilenameEncode(String string)
Encodes the given string by removing chars which are illegal on most file systems.
if (string != null) {
    string = string.replaceAll(ILLEGAL_FILENAME_CHARS_REGEX, "");
    if (string.length() > 255) {
        string = string.substring(0, 255);
return string;