Skip to content

Commit ff6c3ec

Browse files
committed
Add PathUtils.getBaseName(Path)
1 parent f94af4a commit ff6c3ec

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ The <action> type attribute can be add,update,fix,remove.
107107
<action dev="ggregory" type="add" due-to="Gary Gregory">Add FileTimes.toUnixTime(FileTime).</action>
108108
<action dev="ggregory" type="add" due-to="Gary Gregory">Add BrokenInputStream.Builder.</action>
109109
<action dev="ggregory" type="add" due-to="Gary Gregory">Add PathUtils.getExtension(Path).</action>
110+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add PathUtils.getBaseName(Path).</action>
110111
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ThrottledInputStream.</action>
111112
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IORunnable.noop().</action>
112113
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ChecksumInputStream and test #548.</action>

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,7 @@ public static DosFileAttributeView getDosFileAttributeView(final Path path, fina
883883
* </p>
884884
*
885885
* @param path the path to query.
886-
* @return the extension of the file or an empty string if none exists or {@code null}
887-
* if the fileName is {@code null}.
886+
* @return the extension of the file or an empty string if none exists or {@code null} if the fileName is {@code null}.
888887
* @since 2.16.0
889888
*/
890889
public static String getExtension(final Path path) {
@@ -1856,6 +1855,24 @@ public static Path writeString(final Path path, final CharSequence charSequence,
18561855
return path;
18571856
}
18581857

1858+
/**
1859+
* Gets the base name (the part up to and not including the last ".") of the last path segment of a file name.
1860+
* <p>
1861+
* Will return the file name itself if it doesn't contain any dots. All leading directories of the {@code file name} parameter are skipped.
1862+
* </p>
1863+
*
1864+
* @return the base name of file name
1865+
* @param path the path of the file to obtain the base name of.
1866+
* @since 2.16.0
1867+
*/
1868+
public static String getBaseName(final Path path) {
1869+
if (path == null) {
1870+
return null;
1871+
}
1872+
final Path fileName = path.getFileName();
1873+
return fileName != null ? FilenameUtils.removeExtension(fileName.toString()) : null;
1874+
}
1875+
18591876
/**
18601877
* Prevents instantiation.
18611878
*/

src/test/java/org/apache/commons/io/file/PathUtilsTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,27 @@ public void testCreateDirectoriesSymlinkClashing() throws IOException {
246246
assertEquals(symlinkedDir, PathUtils.createParentDirectories(symlinkedDir.resolve("child")));
247247
}
248248

249+
@Test
250+
public void testGetBaseNamePathBaseCases() {
251+
assertEquals("bar", PathUtils.getBaseName(Paths.get("a/b/c/bar.foo")));
252+
assertEquals("foo", PathUtils.getBaseName(Paths.get("foo")));
253+
assertEquals("", PathUtils.getBaseName(Paths.get("")));
254+
assertEquals("", PathUtils.getBaseName(Paths.get(".")));
255+
for (final File f : File.listRoots()) {
256+
assertNull(PathUtils.getBaseName(f.toPath()));
257+
}
258+
if (SystemUtils.IS_OS_WINDOWS) {
259+
assertNull(PathUtils.getBaseName(Paths.get("C:\\")));
260+
}
261+
}
262+
263+
@Test
264+
public void testGetBaseNamePathCornerCases() {
265+
assertNull(PathUtils.getBaseName((Path) null));
266+
assertEquals("foo", PathUtils.getBaseName(Paths.get("foo.")));
267+
assertEquals("", PathUtils.getBaseName(Paths.get("bar/.foo")));
268+
}
269+
249270
@Test
250271
public void testGetExtension() {
251272
assertNull(PathUtils.getExtension(null));

0 commit comments

Comments
 (0)