[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); } }
------------------------------------------------------------------
Always put yourself in the other's shoes.If you feel that it hurts you,it probably hurts others,too.------------------------------------------------------------------