Java File Name Encode filenameEncode(String string)

Here you can find the source of filenameEncode(String string)

Description

Encodes the given string by removing chars which are illegal on most file systems.

License

Open Source License

Parameter

Parameter Description
string the string to encode.

Return

an encoded filename string.

Declaration

public static String filenameEncode(String string) 

Method Source Code

//package com.java2s;

public class Main {
    private static final String ILLEGAL_FILENAME_CHARS_REGEX = "[/\\?%*:|\"'<>.]";

    /**//from   w w  w .  j a v  a2 s .  c o m
     * Encodes the given string by removing chars which are illegal on most file 
     * systems.
     * 
     * @param string the string to encode.
     * @return an encoded filename string.
     */
    public static String filenameEncode(String string) {
        if (string != null) {
            string = string.replaceAll(ILLEGAL_FILENAME_CHARS_REGEX, "");

            if (string.length() > 255) {
                string = string.substring(0, 255);
            }
        }

        return string;
    }
}

Related

  1. fileNameEncode(String fileName)
  2. fileNameEncode(String str)