< prev index next >

src/java.base/share/classes/java/nio/file/Path.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 43,56 **** * The other name elements are directory names. A {@code Path} can represent a * root, a root and a sequence of names, or simply one or more name elements. * A {@code Path} is considered to be an <i>empty path</i> if it consists * solely of one name element that is empty. Accessing a file using an * <i>empty path</i> is equivalent to accessing the default directory of the ! * file system. {@code Path} defines the {@link #getFileName() getFileName}, ! * {@link #getParent getParent}, {@link #getRoot getRoot}, and {@link #subpath ! * subpath} methods to access the path components or a subsequence of its name ! * elements. * * <p> In addition to accessing the components of a path, a {@code Path} also * defines the {@link #resolve(Path) resolve} and {@link #resolveSibling(Path) * resolveSibling} methods to combine paths. The {@link #relativize relativize} * method that can be used to construct a relative path between two paths. --- 43,59 ---- * The other name elements are directory names. A {@code Path} can represent a * root, a root and a sequence of names, or simply one or more name elements. * A {@code Path} is considered to be an <i>empty path</i> if it consists * solely of one name element that is empty. Accessing a file using an * <i>empty path</i> is equivalent to accessing the default directory of the ! * file system. The <em>extension</em> of a {@code Path} is defined to be the ! * portion of the path after but not including the last dot ({@code '.'}) and ! * is part of the name element farthest from the root. {@code Path} defines the ! * {@link #getFileName() getFileName}, {@link #getParent getParent}, ! * {@link #getRoot getRoot}, {@link #getExtension getExtension}, and ! * {@link #subpath subpath} methods to access the path components or a ! * subsequence of its name elements. * * <p> In addition to accessing the components of a path, a {@code Path} also * defines the {@link #resolve(Path) resolve} and {@link #resolveSibling(Path) * resolveSibling} methods to combine paths. The {@link #relativize relativize} * method that can be used to construct a relative path between two paths.
*** 159,168 **** --- 162,201 ---- * @return a path representing the path's parent */ Path getParent(); /** + * Returns the extension of this {@code Path} as a {@code String}. + * The extension is that portion of the path after the last dot + * ({@code '.'}). If this path is empty, does not contain a dot, ends + * with a dot, or has a {@link FileSystem#getSeparator() name-separator} + * after the last dot, then this method returns an empty {@code String}. + * + * @implSpec + * For the cases where a non-empty string would be returned, the default + * implementation is equivalent for this path to: + * <pre>{@code + * toString().substring(toString().lastIndexOf(".") + 1); + * }</pre> + * + * @return the extension of this path or an empty {@code String} + * + * @since 11 + */ + default String getExtension() { + String path = toString(); + + int lastDot = path.lastIndexOf("."); + if (lastDot == -1 || lastDot == path.length() - 1 || + path.indexOf(getFileSystem().getSeparator(), lastDot + 1) != -1) { + return ""; + } + + return path.substring(lastDot + 1); + } + + /** * Returns the number of name elements in the path. * * @return the number of elements in the path, or {@code 0} if this path * only represents a root component */
< prev index next >