Here you can find the source of completeFileExtension(File file, FileFilter filter)
public static File completeFileExtension(File file, FileFilter filter)
//package com.java2s; //License from project: Open Source License import java.io.File; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; public class Main { public static File completeFileExtension(File file, FileFilter filter) { if ((file != null) && (filter != null)) { if (filter instanceof FileNameExtensionFilter) { String[] exts = ((FileNameExtensionFilter) filter) .getExtensions(); String fName = file.getName(); if ((exts != null) && (fName != null)) { int dotPos = fName.indexOf('.'); if ((exts.length == 1) && ((dotPos < 0) || (dotPos == (fName.length() - 1)))) { if (dotPos < 0) { fName += "."; }// w w w.j a v a2 s . co m fName += exts[0]; File parent = file.getParentFile(); if (parent != null) { file = new File(parent, fName); } else { file = new File(fName); } } } } } return file; } }