Android File Name Check isFilenameValid(String file)

Here you can find the source of isFilenameValid(String file)

Description

Only check if a given filename is valid according to the OS rules.

License

Open Source License

Parameter

Parameter Description
file the name of a file

Return

true if the file is valid, false otherwise

Declaration

public static boolean isFilenameValid(String file) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.IOException;

public class Main {
    /**/*from  www .  j  ava2 s .com*/
     * Only check if a given filename is valid according to the OS rules.
     * 
     * You still need to handle other failures when actually creating 
     * the file (e.g. insufficient permissions, lack of drive space, security restrictions). 
     * @param file the name of a file
     * @return true if the file is valid, false otherwise
     */
    public static boolean isFilenameValid(String file) {
        File f = new File(file);
        try {
            f.getCanonicalPath();
            return true;
        } catch (IOException e) {
            return false;
        }
    }
}