If I have a string variable inside one class
MainActivity.selectedFilePath
which matters
/sdcard/images/mr.32.png
and I want to print somewhere only the path to this folder without a file name
/sdcard/images/
You can do:
File theFile = new File("/sdcard/images/mr.32.png"); String parent = theFile.getParent();
Or (less recommended)
String path = "/sdcard/images/mr.32.png"; String parent = path.replaceAll("^(.*)/.*?$","$1");
Take a look
String string = "/sdcard/images/mr.32.png"; int lastSlash = string.lastIndexOf("/"); String result = string.substring(0, lastSlash); System.out.println(result);
(MainActivity.selectedFilePath).getParent(). getAbsolutePath()
File , getPath File Class.
String realPath = "/sdcard/images/mr.32.png"; String myPath = realPath.substring(0, realPath.lastIndexOf("/") + 1);
String.lastIndexOf(int ch);, ch
String.lastIndexOf(int ch);
:
File file = new File("path");
parentPath = file.getParent();
parentDir = file.getParentFile();
, File File.getParent().
, String.split(), String "/" . . .
/ "".
final String dir = System.getProperty("user.dir"); String[] array = dir.split("[\\\\/]",-1) ; String arrval=""; for (int i=0 ;i<array.length;i++) { arrval=arrval+array[i]; } System.out.println(arrval);
Apache FileNameUtils IO Apache Commons , , getFullPath().
-
String selectedFilePath= "/sdcard/images/mr.32.png"; selectedFilePath=selectedFilePath.substring(0,selectedFilePath.lastIndexOf("/")); System.out.println(selectedFilePath);