File file = new File("C:/aaa/bbb/ccc/ffffd/test.java");
File curentPath = new File(file.getParent());
//get current path "C:/aaa/bbb/ccc/ffffd/"
String currentFolder= currentPath.getName().toString();
//get name of file to string "ffffd"
if you need to append folder "ffffd" by another path use;
There is no need to create a File instance to parse the string in groovy. It can be done as follows:
String path = "C:/aaa/bbb/ccc/ffffd/test.java"
path.split('/')[-2] // this will return ffffd
The split will create the array [C:, aaa, bbb, ccc, ffffd, test.java] and index -2 will point to entry before the last one, which in this case is ffffd
If you have just String path and don't want to create new File object you can use something like:
f.getParentFile()
can be null, so you should check it.if you need to append folder "ffffd" by another path use;
From java 7 I would prefer to use Path. You only need to put path into:
and create some get method:
In Groovy:
There is no need to create a
File
instance to parse the string in groovy. It can be done as follows:The split will create the array
[C:, aaa, bbb, ccc, ffffd, test.java]
and index-2
will point to entry before the last one, which in this case isffffd
In Java 7 you have the new Paths api. The modern and cleanest solution is:
Result would be: