Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.4k points)
Just to be clear, I'm not looking for the MIME type.

Let's say I have the following input: /path/to/file/foo.txt

I'd like a way to break this input up, specifically into .txt for the extension. Is there any built-in way to do this in Java? I would like to avoid writing my own parser.

1 Answer

0 votes
by (46k points)

It's very easy, just apply FilenameUtils.getExtension from Apache Commons IO

Here is an example of how to use it (it's up to you whether you want to specify full path or just file name):

String ext1 = FilenameUtils.getExtension("/path/to/file/foo.txt"); // returns "txt"

String ext2 = FilenameUtils.getExtension("bar.exe"); // returns "exe"

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 9, 2019 in Java by prachi95 (1.1k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...