Java File Extension Name Get getFileExtension(File file, boolean withDot)

Here you can find the source of getFileExtension(File file, boolean withDot)

Description

Returns extension of specified file.

License

Open Source License

Parameter

Parameter Description
file File to extract extension from
withDot If is true then returned extension is proceded by a dot.

Return

extension of specified file

Declaration

public static String getFileExtension(File file, boolean withDot) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    /**//w w w.j a v  a 2s .  c om
     * Returns extension of specified file. The extension can be returned with or without preceding dot.
     * @param file File to extract extension from
     * @param withDot If is true then returned extension is proceded by a dot.
     * @return extension of specified file
     */
    public static String getFileExtension(File file, boolean withDot) {
        if (file == null) {
            return null;
        }
        String path = file.getAbsolutePath();
        int dotAt = path.lastIndexOf('.');
        if (dotAt < 0) {
            return null;
        }
        if (!withDot) {
            dotAt++;
        }
        return path.substring(dotAt);
    }
}

Related

  1. getFileExtension(File file)
  2. getFileExtension(File file)
  3. getFileExtension(File file)
  4. getFileExtension(File file)
  5. getFileExtension(File file, boolean includeDot)
  6. getFileExtension(final File aFile)
  7. getFileExtension(final File file)
  8. getFileExtension(final File file)
  9. getFileExtension(final String file)