Here you can find the source of fileNameAutoExtend(FileFilter filter, File file)
public static String fileNameAutoExtend(FileFilter filter, File file)
//package com.java2s; //License from project: LGPL import java.io.File; import javax.swing.filechooser.FileFilter; public class Main { /**/*from w ww . j av a2 s.c o m*/ * If the user does not input the extension specified by the file filter, automatically augment the file name with the specified extension. */ public static String fileNameAutoExtend(FileFilter filter, File file) { if (filter == null) return file.getAbsolutePath(); String description = filter.getDescription().toLowerCase(); String extension = getExtensionInLowerCase(file); String filename = file.getAbsolutePath(); if (extension != null) { if (!filter.accept(file)) { filename = file.getAbsolutePath().concat(".").concat(description); } } else { filename = file.getAbsolutePath().concat(".").concat(description); } return filename; } /** @return the extension of a file name in lower case */ public static String getExtensionInLowerCase(File file) { if (file == null || file.isDirectory()) return null; String extension = getSuffix(file.getName()); if (extension != null) return extension.toLowerCase(); return null; } /** @return the extension of a file name */ public static String getSuffix(String filename) { String extension = null; int index = filename.lastIndexOf('.'); if (index >= 1 && index < filename.length() - 1) { extension = filename.substring(index + 1); } return extension; } }