[Java] 03 String获取文件后缀名,判断文件是否合法

package test.string;

import java.util.Arrays;
import java.util.List; public class GetFileType {
private static List<String> accTypes = Arrays.asList("jpg","bmp","gif","doc","docx","rar");
public static void main(String[] args){
for(String arg:args){
String type = getFileType(arg);
if(accTypes.contains(type)){
System.out.println(arg+"后缀合法");
}else{
System.out.println(arg+"后缀不合法");
}
}
}
/**
* 获取文件后缀名
* @param filename
* @return
*/
public static String getFileType(String filename){
int pos = filename.lastIndexOf(".");
if(pos == -1){
return null;
}
return filename.substring(pos+1);
}
}
上一篇:return 通过文件后缀名得到的函数字符串


下一篇:洛谷P1876 开灯