Java File Name Sanitize sanitizeFilename(String name)

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

Description

Substitute any characters to be avoided in a file name with '_'.

License

Open Source License

Declaration

public static String sanitizeFilename(String name) 

Method Source Code

//package com.java2s;
// under the terms of the GNU General Public License as published by the Free

public class Main {
    /**/*from  w w w.jav a 2  s .  com*/
     * Substitute any characters to be avoided in a file name with '_'.
     */
    public static String sanitizeFilename(String name) {
        if (name.isEmpty())
            throw new IllegalArgumentException("Empty file name!");
        // replace all non-word characters (a word character is: [a-zA-Z_0-9]) except '.' and '-'
        return name.replaceAll("[\\W&&[^.-]]", "_");
    }
}

Related

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