Get file extension in java

I have a problem getting the file name extension. I get 1 file url.
It could be like

/var/www.dir/file.tar.gz OR /var/www.dir/file.exe 

Now I need to split the file extension (e.g. .tar.gz OR.exe) from the given URL. The URL is set dynamically by the user. Anyone please help me how to solve this problem.

+4
source share
8 answers

This is not a terrible challenge, but a retry. It seems like it can be produced quickly, and it is possible. But for this you need to use ready-made, proven libraries: Apo IO Tools , in particular FilenameUtils # getExtension . This way you avoid creepy mistakes or inconsistencies. In addition, you get many other useful, proven materials.

Of course, in the case of a double file extension, you may need to use the methods more than once, since the library cannot guess at what level you need the extensions. This is a .gz file, only after un-gunzipping is a .tar file.

+7
source

You can use a combination of lastIndexOf and indexOf :

 String s = "/var/www.dir/file.tar.gz"; String file = s.substring(s.lastIndexOf("/")); String extension = file.substring(file.indexOf(".")); // .tar.gz 

Demo .

+4
source

You can do something like:

 String str = "/var/www.dir/file.tar.gz"; Matcher m = Pattern.compile(".*/.*?(\\..*)").matcher(str); if (m.matches()) { fileExtension = m.group(1); } 
+3
source

It seems that the easiest solution for this would be to create separate cases for outliers (e.g. .tar.gz) and treat the rest as one and the same (i.e. handle everything after the last character . ).

Extensions with multiple characters . they are so unusual that you don’t need a minimized method to analyze what you need. So:

 int length = url.length(); String extension = null; if(url.substring(length-7).equalsIgnoreCase(".tar.gz")) { //special case } else { extension = url.substring(url.lastIndexOf(".")); } 
0
source

I just realized that I read your question incorrectly. You can compile extensions in a similar method, though

 String yourString = "/your/whole/path.ext.si.ons"; String[] pathPieces = yourString.split("/"); // [ "your", "whole", "path.ext.si.ons" ] String lastPiece = pathPieces[pathPieces.length - 1]; String[] namePieces = lastPiece.split("."); // [ "path", "ext", "si", "ions" ] 

At this point, the first part will probably be your name, where the rest of the parts will be extensions. Then you can join them to have your type of extension.

0
source

This code can be used to get the file extension.

 public static String getFileExtension(String filename) { if (filename == null) { return null; } int lastUnixPos = filename.lastIndexOf('/'); int lastWindowsPos = filename.lastIndexOf('\\'); int indexOfLastSeparator = Math.max(lastUnixPos, lastWindowsPos); int extensionPos = filename.lastIndexOf('.'); int lastSeparator = indexOfLastSeparator; int indexOfExtension = lastSeparator > extensionPos ? -1 : extensionPos; int index = indexOfExtension; if (index == -1) { return ""; } else { return filename.substring(index + 1); } } 
0
source

Assuming the URL pattern should be: "/somurl/blah-blah/filename.extension" . I would suggest the following code snippet as a solution to your problem.

 String getFileExtension(String url) { String[] urlSplit = url.split("/"); String filename = urlSplit[urlSplit.length - 1]; String[] nameSplit = filename.split("[.]"); StringBuffer fileExtension = new StringBuffer(); // to prevent appending . after extension type if (nameSplit.length > 1) { for (int index = 1; index < nameSplit.length; index++) { if (index != nameSplit.length - 1) fileExtension.append(nameSplit[index] + "."); else fileExtension.append(nameSplit[index]); } } else { fileExtension.append(nameSplit[0]); } return fileExtension.toString(); } 
0
source

Get the file input, then split it with. as a separator maybe? to set the separator:

 Scanner line_scanner = new Scanner(line).useDelimiter("."); 

read the file url twice, once for the file name and once for the extension.

-1
source

Source: https://habr.com/ru/post/1435152/


All Articles