Java Is File Name Valid isValidFileName(String fileName)

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

Description

This method checks if a String represents a valid File name.

License

Open Source License

Parameter

Parameter Description
fileName the File name

Return

true if the File name is valid

Declaration

public static boolean isValidFileName(String fileName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * <copyright> Copyright (c) 2009-2014 Bauhaus Luftfahrt e.V.. All rights reserved. This program and the accompanying
 * materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html </copyright>
 *******************************************************************************/

import java.io.File;
import java.io.IOException;

public class Main {
    /**/*w  w  w .j  av a2 s.  co  m*/
     * This method checks if a String represents a valid File name.
     * 
     * @param fileName the File name
     * @return true if the File name is valid
     */
    public static boolean isValidFileName(String fileName) {

        File f = new File(fileName);
        try {
            f.getCanonicalPath();
            return true;
        } catch (IOException e) {
            return false;
        }

    }

    /**
     * This method checks weather a File name is valid and has a certain suffix.
     * 
     * @param fileName the File name
     * @param suffix the File name suffix
     * @return true if the File name is valid and has the given suffix
     */
    public static boolean isValidFileName(String fileName, String suffix) {
        if (fileName.endsWith(suffix)) {
            return isValidFileName(fileName);
        }
        return false;
    }
}

Related

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