Android File Extension Name Get getNameMinusExtension(File file)

Here you can find the source of getNameMinusExtension(File file)

Description

Returns the file's name minus any extension, that is, the part of its name up to (but not including) the last '.'

License

Open Source License

Parameter

Parameter Description
file the File whose name is will be returned

Exception

Parameter Description
IllegalArgumentException if file == null

Declaration

public static String getNameMinusExtension(File file)
        throws IllegalArgumentException 

Method Source Code

/*//from w ww .jav a2s .c om
Copyright ? 2008 Brent Boyer

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project).  If not, see <http://www.gnu.org/licenses/>.
 */

import bb.science.FormatUtil;
import bb.util.Check;
import bb.util.StringUtil;
import bb.util.ThrowableUtil;
import bb.util.logging.LogUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.Random;
import java.util.logging.Level;
import org.junit.Assert;
import org.junit.Test;

public class Main{
    /**
     * Returns the file's name minus any extension, that is, the part of its name up to (but not including) the last '.' char.
     * For example, if presented with a file named "helloWorld.old.txt" then "helloWorld.old" is returned.
     * If no extension exists (either because there is nothing after the last '.' char or a '.' char never occurs)
     * then the complete file name (up to but not including any final '.' char) is returned.
     * Note that the File need not actually exist nor be a normal file.
     * <p>
     * @param file the File whose name is will be returned
     * @throws IllegalArgumentException if file == null
     */
    public static String getNameMinusExtension(File file)
            throws IllegalArgumentException {
        Check.arg().notNull(file);

        String name = file.getName();
        int indexPeriod = name.lastIndexOf('.');
        if (indexPeriod == -1)
            return name;
        else
            return name.substring(0, indexPeriod);
    }
}

Related

  1. getFileExtension(String filename, String defExt)
  2. getFileExtensionFromName(String filename)
  3. getFileExtensionFromSource(byte[] picHeader)
  4. getFileExtensionFromUrl(String url)
  5. getFilenameExtension(String path)
  6. getPathExtension(final String path)
  7. getSuffix(String f)
  8. hasExtension(String filename, String extension)
  9. indexOfExtension(String filename)