Java Utililty Methods File Name Clean

List of utility methods to do File Name Clean

Description

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

Method

StringcleanFileName(final String originalFileName)
Replace all characters that could give file system problems with underscores.
final int controlCharactersLimit = 32;
final int ruboutCharacter = 127;
final StringBuilder cleanName = new StringBuilder();
for (final char character : originalFileName.toCharArray()) {
    final boolean controlCharacter = character < controlCharactersLimit || character == ruboutCharacter;
    cleanName.append(!controlCharacter && "\\/<>:\"|?*".indexOf(character) == -1 ? character : "_");
return cleanName.toString();
...
StringcleanFileName(final String value)
clean File Name
assert value != null;
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
    final char ch = value.charAt(i);
    if ((ch >= 'A') && (ch <= 'z')) {
        sb.append(ch);
    if ((ch >= '0') && (ch <= '9')) {
...
StringcleanFileName(String arg)
clean up file name so that underlying OS can open file spec
if (arg == null)
    return arg;
StringBuilder sb = new StringBuilder();
boolean quoteNeeded = false;
for (int i = 0; i < arg.length(); i++) {
    char c = arg.charAt(i);
    sb.append(c);
    if (c == ' ')
...
StringcleanFileName(String badFileName)
Conservative approach to insuring that a given filename only contains characters that are legal for use in filenames on the disk.
StringBuilder cleanName = new StringBuilder();
for (int i = 0; i < badFileName.length(); i++) {
    int c = (int) badFileName.charAt(i);
    if (Character.isJavaIdentifierPart(c))
        cleanName.append((char) c);
    else
        cleanName.append('_');
return cleanName.toString();
StringcleanFilename(String filename)
clean Filename
byte[] fnBytes = filename.getBytes();
byte c;
for (int i = 0; i < fnBytes.length; i++) {
    c = fnBytes[i];
    if (VALID_FILENAME_CHARS.indexOf(c) == -1) {
        fnBytes[i] = '_';
return new String(fnBytes);
StringcleanFileName(String filename)
Clean given 'filename' in order to be able to be used as a file name.
return filename.replace(':', '.').replace('/', '_');
StringcleanFilename(String filename)
clean Filename
filename = filename.toLowerCase();
filename.replaceAll("/[ _a-z0-9^]/", "");
filename.replaceAll(" ", "_");
return filename;
StringcleanFileName(String name)
clean File Name
String cleanName = name;
for (String s : ILLEGAL_FILE_NAME_CHARACTERS) {
    if (cleanName.contains(s))
        cleanName = cleanName.replace(s, "_");
return cleanName;
StringcleanFileName(String name)
clean File Name
return name.substring(0, name.lastIndexOf(".")).replaceAll("[^a-zA-Z0-9]", "");
StringcleanFileName(String path)
clean File Name
int index = path.lastIndexOf('/');
if (index == -1) {
    index = path.lastIndexOf('\\');
return (index != -1 ? path.substring(index + 1) : path);