代码实现
public class FileUtil {
/**
* 输入file对象返回文件前缀名
* @param filePath
* @return
*/
public static String getFilePrefix(File file){
String fileName = file.getName();
if(fileName == null || "".equals(fileName)){
return null;
}
String filePrefix = fileName.substring(0,fileName.lastIndexOf("."));
return filePrefix;
}
/**
* 输入文件名返回文件前缀名
* @param fileName
* @return
*/
public static String getFilePrefix(String fileName){
if(fileName == null || "".equals(fileName)){
return null;
}
String filePrefix = fileName.substring(0,fileName.lastIndexOf("."));
return filePrefix;
}
/**
* 输入文件名返回文件后缀名
* @param fileName
* @return
*/
public static String getFileSufix(String fileName){
if(fileName == null || "".equals(fileName)){
return null;
}
String fileSufix = fileName.substring(fileName.lastIndexOf(".")+1);
return fileSufix;
}
public static void main(String[] args) {
System.out.println(getFilePrefix(new File("G:/a.txt")));
System.out.println(getFileSufix("a.txt"));
}
}