xyz.proteanbear.template.utils.FileSuffix Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of PbPOITemplate Show documentation
Show all versions of PbPOITemplate Show documentation
The tools is created for easy use of Apache POI.
The newest version!
package xyz.proteanbear.template.utils;
import xyz.proteanbear.template.exception.FileSuffixNotSupportException;
import java.io.File;
/**
* File extension name
*
* @author ProteanBear
*/
public enum FileSuffix
{
EXCEL_XLSX(".xlsx"),
EXCEL_XLS(".xls"),
WORD_DOC(".doc"),
WORD_DOCX(".docx");
/**
*
*/
private final String suffix;
/**
* @param suffix the file suffix
*/
FileSuffix(String suffix)
{
this.suffix=suffix;
}
/**
* @param name file name
* @return the file suffix
*/
public boolean check(String name)
{
return name!=null && (name.endsWith(suffix));
}
/**
* Get the file type suffix.
*
* @param file file object
* @return file type suffix
* @throws FileSuffixNotSupportException Throw exception if file type is not supported
*/
public static FileSuffix getBy(File file) throws FileSuffixNotSupportException
{
String fileName=file.getName();
if(EXCEL_XLSX.check(fileName)) return EXCEL_XLSX;
if(EXCEL_XLS.check(fileName)) return EXCEL_XLS;
if(WORD_DOC.check(fileName)) return WORD_DOC;
if(WORD_DOCX.check(fileName)) return WORD_DOCX;
throw new FileSuffixNotSupportException();
}
}