tl;dr

pathToFile.getParent()

Path#getParent

No need to resort to crude string manipulation. Java NIO abstracts us away from the platform-specific details.

Call Path#getParent to get another path object while dropping the last element of the original.

Path pathToFile = Paths.get( "SomeFolder" , "SomeFile" ) ;
Path pathToFolder = pathToFile.getParent() ;

List#removeLast

By the way, to answer the title of your Question…

In Java 21+, List is a sub-interface of SequencedCollection. This brings convenient new methods such as removeLast.

myList.removeLast() ;
Answer from Basil Bourque on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › linkedlist-removelast-method-in-java
LinkedList removeLast() Method in Java - GeeksforGeeks
July 11, 2025 - In Java, the removeLast() method of LinkedList class is used to remove and return the last element of the list.
🌐
TutorialsPoint
tutorialspoint.com › java › util › linkedlist_removelast.htm
Java LinkedList removeLast() Method
The Java LinkedList removeLast() method retrieves and removes the last element of this linkedList. This operation modifies the LinkedList instance.
🌐
TutorialsPoint
tutorialspoint.com › java › util › arraydeque_removelast.htm
Java ArrayDeque removeLast() Method with Examples
The Java ArrayDeque removeLast() method retrieves and removes the last element of this deque.
🌐
Tabnine
tabnine.com › home page › code › java › java.util.linkedlist
java.util.LinkedList.removeLast java code examples | Tabnine
private static void addToReversePostOrderListIterative(DirectNode root, List<DirectNode> lst) { LinkedList<DirectNode> stackNode = new LinkedList<>(); LinkedList<Integer> stackIndex = new LinkedList<>(); HashSet<DirectNode> setVisited = new HashSet<>(); stackNode.add(root); stackIndex.add(0); while (!stackNode.isEmpty()) { DirectNode node = stackNode.getLast(); int index = stackIndex.removeLast(); setVisited.add(node); for (; index < node.succs.size(); index++) { DirectNode succ = node.succs.get(index); if (!setVisited.contains(succ)) { stackIndex.add(index + 1); stackNode.add(succ); stackIndex.add(0); break; } } if (index == node.succs.size()) { lst.add(0, node); stackNode.removeLast(); } } }
🌐
GitHub
github.com › jumpalottahigh › JavaChallenge-MOOC-course › blob › master › week3-062.RemoveLast › src › RemoveLast.java
JavaChallenge-MOOC-course/week3-062.RemoveLast/src/RemoveLast.java at master · jumpalottahigh/JavaChallenge-MOOC-course
import java.util.Collections; · public class RemoveLast { public static void removeLast(ArrayList<String> list) { int i = list.size(); list.remove(i-1); } · public static void main(String[] args) { // Here an example what you can do with the method ·
Author   jumpalottahigh
Find elsewhere
🌐
Java Guides
javaguides.net › 2024 › 06 › java-arraylist-removelast-method.html
Java ArrayList removeLast() Method
June 11, 2024 - The removeLast() method can be used to simplify this operation. import java.util.ArrayList; import java.util.List; class HistoryEntry { String url; HistoryEntry(String url) { this.url = url; } @Override public String toString() { return url; } } public class BrowserHistory { public static void main(String[] args) { List<HistoryEntry> history = new ArrayList<>(); history.add(new HistoryEntry("http://example.com/page1")); history.add(new HistoryEntry("http://example.com/page2")); history.add(new HistoryEntry("http://example.com/page3")); // Remove the last history entry HistoryEntry lastEntry = ((ArrayList<HistoryEntry>) history).removeLast(); System.out.println("Removed history entry: " + lastEntry); System.out.println("Remaining history: " + history); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraydeque-removelast-method-in-java
ArrayDeque removeLast() Method in Java - GeeksforGeeks
December 10, 2018 - The Java.util.ArrayDeque.removeLast() method is used to remove the last element of the Deque. Syntax: Array_Deque.removeLast() Parameters: The method does not take any parameters.
🌐
Tabnine
tabnine.com › home page › code › java › java.util.deque
java.util.Deque.removeLast java code examples | Tabnine
private void processColorCode(char c) { if (c == FontColor.getReset()) { if (!previousColors.isEmpty()) { currentColor = previousColors.removeLast(); } } else { previousColors.addLast(currentColor); currentColor = FontColor.toColor(c); } } ... /** * An analogue of {@link java.util.function.DoubleFunction} also accepting an index.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › LinkedList.html
LinkedList (Java Platform SE 8 )
2 weeks ago - public E removeLast() Removes and returns the last element from this list. Specified by: removeLast in interface Deque<E> Returns: the last element from this list · Throws: NoSuchElementException - if this list is empty · public void addFirst(E e) Inserts the specified element at the beginning ...
🌐
Educative
educative.io › answers › what-is-the-linkedlistremovelast-method-in-java
What is the LinkedList.removeLast() method in Java?
The code below demonstrates how to use the removeLast() method: import java.util.LinkedList; class LinkedListRemoveLast { public static void main( String args[] ) { LinkedList<String> list = new LinkedList<>(); list.add("1"); list.add("2"); list.add("3"); System.out.println("The list is " + list); System.out.println("Removing last element of the list : " + list.removeLast()); System.out.println("The list is " + list); } } Run ·
🌐
W3Resource
w3resource.com › java-tutorial › util › arraydeque › java_arraydeque_removelast.php
Java ArrayDeque Class: removeLast() Method - w3resource
Java ArrayDeque Class: removeLast() Method with example: Retrieves and removes the last element of this deque. This method differs from pollLast only in that it throws an exception if this deque is empty.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.linkedlist.removelast
LinkedList.RemoveLast Method (Java.Util) | Microsoft Learn
[<Android.Runtime.Register("removeLast", "()Ljava/lang/Object;", "GetRemoveLastHandler")>] abstract member RemoveLast : unit -> Java.Lang.Object override this.RemoveLast : unit -> Java.Lang.Object · Object · the last element from this list · RemoveLast() ISequencedCollection.RemoveLast() Attributes ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › removing-last-element-from-arraylist-in-java
Removing last element from ArrayList in Java - GeeksforGeeks
July 12, 2025 - We can use the remove() method of ArrayList container in Java to remove the last element.
🌐
AlphaCodingSkills
alphacodingskills.com › java › notes › java-linkedlist-removelast.php
Java LinkedList removeLast() Method - AlphaCodingSkills
The java.util.LinkedList.removeLast() method is used to remove and return the last element of the list. Every removal of element results into reducing the list size by one unless the list is empty. public E removeLast() Here, E is the type of element maintained by the container.