java获取短文件名和扩展名
?
/** * 获取短文件名,不带扩展名 * @param fileName * @return */ public static String getShortName(String fileName){ if(fileName != null && fileName.length()>0 && fileName.lastIndexOf(".")>-1){ return fileName.substring(0, fileName.lastIndexOf(".")); } return fileName; } /** * 获取扩展名,带点 * @param fileName * @return */ public static String getExtention(String fileName){ if(fileName!=null && fileName.length()>0 && fileName.lastIndexOf(".")>-1){ return fileName.substring(fileName.lastIndexOf(".")); } return ""; } public static void main(String[] args) { System.out.println(FileUtil.getShortName("aaa.xlsx")); System.out.println(FileUtil.getShortName("aaa")); System.out.println(FileUtil.getShortName(".")); System.out.println(FileUtil.getShortName(".xlsx")); System.out.println(FileUtil.getExtention("aaa.xlsx")); System.out.println(FileUtil.getExtention("aaa")); System.out.println(FileUtil.getExtention(".")); System.out.println(FileUtil.getExtention(".xlsx")); }
?