How do I get the file name from a String containing the Absolute file path?
just use File.getName()
File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");
System.out.println(f.getName());
using String methods:
File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");
System.out.println(f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf("\\")+1));
How do I get the folder name from a String containing the Absolute file path in android?
new File(path).getParentFile().getName()
should work.
With regards to your current code, don't implement your own path parser. Use File
.
Also note that this has nothing to do with Android specifically; this is a general Java question.
Extract file name from path, no matter what the os/path format
Using os.path.split
or os.path.basename
as others suggest won't work in all cases: if you're running the script on Linux and attempt to process a classic windows-style path, it will fail.
Windows paths can use either backslash or forward slash as path separator. Therefore, the ntpath
module (which is equivalent to os.path when running on windows) will work for all(1) paths on all platforms.
import ntpath
ntpath.basename("a/b/c")
Of course, if the file ends with a slash, the basename will be empty, so make your own function to deal with it:
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
Verification:
>>> paths = ['a/b/c/', 'a/b/c', '\\a\\b\\c', '\\a\\b\\c\\', 'a\\b\\c',
... 'a/b/../../a/b/c/', 'a/b/../../a/b/c']
>>> [path_leaf(path) for path in paths]
['c', 'c', 'c', 'c', 'c', 'c', 'c']
(1) There's one caveat: Linux filenames may contain backslashes. So on linux, r'a/b\c'
always refers to the file b\c
in the a
folder, while on Windows, it always refers to the c
file in the b
subfolder of the a
folder. So when both forward and backward slashes are used in a path, you need to know the associated platform to be able to interpret it correctly. In practice it's usually safe to assume it's a windows path since backslashes are seldom used in Linux filenames, but keep this in mind when you code so you don't create accidental security holes.
String manipulation - getting file name from absolute file path
set filename [file rootname [file tail $file]]
file tail
returns the part after the last /
(not counting trailing /
s), and file rootname
the part before the last .
.
man page for file
Get file name from absolute path in Nodejs?
Use the basename
method of the path
module:
path.basename('/foo/bar/baz/asdf/quux.html')
// returns
'quux.html'
Here is the documentation the above example is taken from.
how can I get file name and path in java?
public static String getFirstZipFilename(File dir) {
for (File file : dir.listFiles()) {
String filePath = file.getPath();
if (file.isFile() && filePath.endsWith(".zip")) {
return filePath;
}
}
return null;
}
- Works with any directory (try to make your utility methods generic...)
- Returns as soon as a valid file has been found (no useless tests)
- Returns null if nothing was found, so you can know it and display warning messages
How to get file name from file path in android
I think you can use substring method to get name of the file from the path string.
String path=":/storage/sdcard0/DCIM/Camera/1414240995236.jpg";
// it contains your image path...I'm using a temp string...
String filename=path.substring(path.lastIndexOf("/")+1);
Java - How to get the name of a file from the absolute path and remove its file extension?
If you're working strictly with file paths, try this
String path = "C:\\Users\\Ewen\\AppData\\Roaming\\MyProgram\\Test.txt";
File f = new File(path);
System.out.println(f.getName()); // Prints "Test.txt"
Thanks but I also want to remove the .txt
OK then, try this
String fName = f.getName();
System.out.println(fName.substring(0, fName.lastIndexOf('.')));
Please see this for more information.
How to get the file name from a full path using JavaScript?
var filename = fullPath.replace(/^.*[\\\/]/, '')
This will handle both \ OR / in paths
Related Topics
"Int Cannot Be Dereferenced" in Java
How to Install the Jdk on Ubuntu Linux
The Simplest Way to Comma-Delimit a List
Shared Memory Between Two Jvms
Connecting an Input Stream to an Outputstream
Java, "Variable Name" Cannot Be Resolved to a Variable
Is Null Check Needed Before Calling Instanceof
Webdriver: Check If an Element Exists
Hibernate Criteria: Joining Table Without a Mapped Association
How to Make Notepad to Save Text in Utf-8 Without the Bom
Generic Type Parameter Naming Convention for Java (With Multiple Chars)
Gradle: Could Not Determine Java Version from '11.0.2'
Springboot 2.6.0/Spring Fox 3 - Failed to Start Bean 'Documentationpluginsbootstrapper'