--- old/src/java.base/share/classes/java/nio/file/Path.java 2018-02-15 08:44:12.000000000 -0800 +++ new/src/java.base/share/classes/java/nio/file/Path.java 2018-02-15 08:44:12.000000000 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -45,10 +45,13 @@ * A {@code Path} is considered to be an empty path if it consists * solely of one name element that is empty. Accessing a file using an * empty path 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. + * file system. The extension 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. * *

In addition to accessing the components of a path, a {@code Path} also * defines the {@link #resolve(Path) resolve} and {@link #resolveSibling(Path) @@ -161,6 +164,36 @@ 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: + *

{@code
+     *     toString().substring(toString().lastIndexOf(".") + 1);
+     * }
+ * + * @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