Android File Extension Name Get getExtension(File file, boolean useFirstPeriod, boolean includePeriod)

Here you can find the source of getExtension(File file, boolean useFirstPeriod, boolean includePeriod)

Description

Returns the file's extension.

License

Open Source License

Parameter

Parameter Description
file the File whose extension will be returned
useFirstPeriod if true then the extension starts at the <i>first</i> occurence of a period (i.e. '.' char) in file's name, otherwise the extension starts at the <i>last</i> occurence of a period in file's name
includePeriod if true, then the period is included in the result, otherwise it is excluded

Exception

Parameter Description
IllegalArgumentException if file is null

Declaration

public static String getExtension(File file, boolean useFirstPeriod,
        boolean includePeriod) throws IllegalArgumentException 

Method Source Code

/*//from www  .  j ava2 s . com
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 extension, defined here as the part of its name after the last '.' char.
     * <p>
     * This is a convenience method which simply returns
     * <code>{@link #getExtension(File, boolean, boolean) getExtension}(file, false, false)</code>.
     * So, all the rules discussed for that method apply here.
     * For example, if presented with a file named "helloWorld.old.txt" then "txt" is returned.
     * If no extension exists (either because there is nothing after the last '.' char or a '.' char never occurs)
     * then the blank string "" is returned.
     * Note that the File need not actually exist nor be a normal file.
     * <p>
     * @param file the File whose extension will be returned
     * @throws IllegalArgumentException if file is null
     */
    public static String getExtension(File file)
            throws IllegalArgumentException {
        return getExtension(file, false, false);
    }
    /**
     * Returns the file's extension.
     * <p>
     * If useFirstPeriod is true, then the extension starts at the <i>first</i> occurence of a period (i.e. '.' char) in file's name,
     * otherwise the extension starts at the <i>last</i> occurence of a period in file's name.
     * If includePeriod is true, then the period is included in the result, otherwise it is excluded.
     * For example, if presented with a file named "helloWorld.old.txt", then the following table shows what is returned
     * for the various combinations of useFirstPeriod and includePeriod:
          <!-- http://www.w3.org/TR/REC-html40/struct/tables.html -->
       <table border="1">
          <tr>
             <th rowspan="2" colspan="2"></th>
             <th colspan="2">includePeriod</th>
          </tr>
          <tr>
             <th><i>true</i></th>
             <th><i>false</i></th>
          </tr>
          <tr>
             <th rowspan="2">useFirstPeriod</th>
             <th><i>true</i></th>
             <td><tt>.old.txt</tt></td>
             <td><tt>old.txt</tt></td>
          </tr>
          <tr>
             <th><i>false</i></th>
             <td><tt>.txt</tt></td>
             <td><tt>txt</tt></td>
          </tr>
       </table>
     * <p>
     * If no extension exists (i.e. there is no '.' char) then the blank string "" is returned.
     * Note that the File need not actually exist nor be a normal file.
     * <p>
     * @param file the File whose extension will be returned
     * @param useFirstPeriod if true then the extension starts at the <i>first</i> occurence of a period (i.e. '.' char) in file's name,
     * otherwise the extension starts at the <i>last</i> occurence of a period in file's name
     * @param includePeriod if true, then the period is included in the result, otherwise it is excluded
     * @throws IllegalArgumentException if file is null
     */
    public static String getExtension(File file, boolean useFirstPeriod,
            boolean includePeriod) throws IllegalArgumentException {
        Check.arg().notNull(file);

        String name = file.getName();

        int indexPeriod = useFirstPeriod ? name.indexOf('.') : name
                .lastIndexOf('.');
        if (indexPeriod == -1)
            return "";

        if (!includePeriod)
            indexPeriod += 1;

        return name.substring(indexPeriod);
    }
}

Related

  1. changeExtension(File file, String extensionNew)
  2. getExtension(File file)
  3. getExtension(File file)
  4. getExtension(String file)
  5. getExtension(String filename)
  6. getExtensionName(String fileName)
  7. getFileEx(String filePath)