How to get ddd
from the path name where the test.java resides.
File file = new File("C:/aaa/bbb/ccc/ddd/test.java");
How to get ddd
from the path name where the test.java resides.
File file = new File("C:/aaa/bbb/ccc/ddd/test.java");
Use File
's getParentFile()
method and String.lastIndexOf()
to retrieve just the immediate parent directory.
Mark's comment is a better solution thanlastIndexOf()
:
file.getParentFile().getName();
These solutions only works if the file has a parent file (e.g., created via one of the file constructors taking a parent File
). When getParentFile()
is null you'll need to resort to using lastIndexOf
, or use something like Apache Commons' FileNameUtils.getFullPath()
:
FilenameUtils.getFullPathNoEndSeparator(file.getAbsolutePath());
=> C:/aaa/bbb/ccc/ddd
There are several variants to retain/drop the prefix and trailing separator. You can either use the same FilenameUtils
class to grab the name from the result, use lastIndexOf
, etc.
Since Java 7 you have the new Paths api. The modern and cleanest solution is:
Paths.get("C:/aaa/bbb/ccc/ddd/test.java").getParent().toString();
Result would be:
C:/aaa/bbb/ccc/ddd
File f = new File("C:/aaa/bbb/ccc/ddd/test.java");
System.out.println(f.getParentFile().getName())
f.getParentFile()
can be null, so you should check it.
Use below,
File file = new File("file/path");
String parentPath = file.getAbsoluteFile().getParent();
If you have just String path and don't want to create new File object you can use something like:
public static String getParentDirPath(String fileOrDirPath) {
boolean endsWithSlash = fileOrDirPath.endsWith(File.separator);
return fileOrDirPath.substring(0, fileOrDirPath.lastIndexOf(File.separatorChar,
endsWithSlash ? fileOrDirPath.length() - 2 : fileOrDirPath.length() - 1));
}
File file = new File("C:/aaa/bbb/ccc/ddd/test.java");
File curentPath = new File(file.getParent());
//get current path "C:/aaa/bbb/ccc/ddd/"
String currentFolder= currentPath.getName().toString();
//get name of file to string "ddd"
if you need to append folder "ddd" by another path use;
String currentFolder= "/" + currentPath.getName().toString();
From java 7 I would prefer to use Path. You only need to put path into:
Path dddDirectoryPath = Paths.get("C:/aaa/bbb/ccc/ddd/test.java");
and create some get method:
public String getLastDirectoryName(Path directoryPath) {
int nameCount = directoryPath.getNameCount();
return directoryPath.getName(nameCount - 1);
}
In Groovy:
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/ddd/test.java"
path.split('/')[-2] // this will return ddd
The split will create the array [C:, aaa, bbb, ccc, ddd, test.java]
and index -2
will point to entry before the last one, which in this case is ddd
For Kotlin :
fun getFolderName() {
val uri: Uri
val cursor: Cursor?
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf(MediaStore.Audio.AudioColumns.DATA)
cursor = requireActivity().contentResolver.query(uri, projection, null, null, null)
if (cursor != null) {
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DATA)
}
while (cursor!!.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data)
val fileName: String = File(absolutePathOfImage).parentFile.name
}
}
//get the parentfolder name
File file = new File( System.getProperty("user.dir") + "/.");
String parentPath = file.getParentFile().getName();