Java Path File Extention nio fileExtension(final Path thePath)

Here you can find the source of fileExtension(final Path thePath)

Description

Gets the extension of the given file.

License

Open Source License

Parameter

Parameter Description
thePath path of the file to check the extension

Return

the extension of the file

Declaration

public static String fileExtension(final Path thePath) 

Method Source Code

//package com.java2s;
/*//from   w  w  w. j a va 2s  .c  om
 * Copyright (c) 2013 christianr.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0.html
 * 
 * Contributors:
 *     christianr - initial API and implementation
 */

import java.nio.file.Path;

public class Main {
    /**
     * Gets the extension of the given file.
     * @param thePath path of the file to check the extension
     * @return the extension of the file
     */
    public static String fileExtension(final Path thePath) {
        if (thePath == null)
            return null;
        if (thePath.getFileName() == null)
            return null;
        String myPathString = thePath.getFileName().toString();
        if (myPathString.lastIndexOf(".") < 0)
            return null;
        return myPathString.substring(myPathString.lastIndexOf(".") + 1,
                myPathString.length());
    }
}

Related

  1. asPaths(final Iterable files)
  2. changeExtension(Path source, String extension)
  3. changeFileExtension(Path fileName, String extension)
  4. fileRenamer(final String fromPath, final String toPath, @Nullable final String toExtension, final boolean junkPaths)
  5. findFilesWithExtension(Path directory, String targetExtension)
  6. getAllPaths(Path root, final String... extensions)
  7. getExtension(final Path path)