Monday 16 May 2016

3 ways to get file name without extension

Using apache commons
import org.apache.commons.io.FilenameUtils;

String fileNameWithoutExt = FilenameUtils.getBaseName(fileName);

                           OR

String fileNameWithOutExt = FilenameUtils.removeExtension(fileName);
Using Google Guava (If u already using it)
import com.google.common.io.Files;
String fileNameWithOutExt = Files.getNameWithoutExtension(fileName);
Or using Core Java
String fileName = file.getName();
int pos = fileName.lastIndexOf(".");
if (pos > 0) {
    fileName = fileName.substring(0, pos);
}

No comments:

Post a Comment