skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, October 18, 2012

Check whether File name is Valid or not

Posted by Raju Gupta at 12:00 PM – 0 comments
 

This function checks for the special characters in a string to make the passed string as a valid file name the special characters checked are ~`!@#$%^&*()+=<>?:\"{}|,/;'[]\

 public static String makeValidFileName(String fileName) {
    int strLength = fileName.length();
    char[] charArray = new char[strLength];
    fileName.getChars(0, strLength, charArray, 0); //Convert the passed string into the array of chars
    StringBuffer newString = new StringBuffer();
    for (int i = 0; i < strLength; ++i) {
      //if the current char of the passed string contains the special char then dont add it to the string buffer
      //else add it to the string buffer
      if (! (specialCharacters.indexOf(charArray[i], 0) > 0)) {
        newString.append(charArray[i]);
      }
    }
    return newString.toString();
  }


Labels: Validation Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger