Java Is File Name Valid isValidFileName(String fileName)

Here you can find the source of isValidFileName(String fileName)

Description

is Valid File Name

License

Open Source License

Declaration

public static boolean isValidFileName(String fileName) 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.IOException;

public class Main {
    private static final char[] ILLEGAL_CHARACTERS = { '`', '?', '*', '\\', '<', '>', '|', '\"', ':', '/', '\n',
            '\r', '\t', '\0', '\f' };

    public static boolean isValidFileName(String fileName) {
        if (fileName == null || fileName.length() == 0) {
            return false;
        }//  w  w  w.ja va 2  s .  c om

        for (char c : ILLEGAL_CHARACTERS) {
            if (fileName.indexOf(c) != -1) {
                return false;
            }
        }
        File f = new File(fileName);
        try {
            f.getCanonicalPath();
            return true;
        } catch (IOException e) {
            return false;
        }
    }
}

Related

  1. isValidFileName(final String aFileName)
  2. isValidFileName(String fileName)
  3. isValidFileName(String input)
  4. isValidFileName(String paramString)
  5. isValidFilePath(String filePath)