Java File Name Clean cleanFileName(String badFileName)

Here you can find the source of cleanFileName(String badFileName)

Description

Conservative approach to insuring that a given filename only contains characters that are legal for use in filenames on the disk.

License

Open Source License

Declaration

public static String cleanFileName(String badFileName) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from ww  w. j a  v a2  s.c om*/
     * Conservative approach to insuring that a given filename only contains characters that are legal for use in filenames on the disk. Other
     * characters are replaced with _ .
     */
    public static String cleanFileName(String badFileName) {
        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();
    }
}

Related

  1. cleanFileName(final String originalFileName)
  2. cleanFileName(final String value)
  3. cleanFileName(String arg)
  4. cleanFilename(String filename)
  5. cleanFileName(String filename)
  6. cleanFilename(String filename)
  7. cleanFileName(String name)