An example logic on getting and printing extension of a file in Java using String handling and Java IO.
Example
Main Logic
lastIndexOf('.'): This method present in the String class returns the last index of the character '.'. For example, if a file name is like file.n.ex then the lastIndexOf('.') will return, 6 but not 4. It is obvious.
substring(name.lastIndexOf('.'),name.length()): The substring method present in the String class, takes start and end positions as parameters, this method returns a string between the start and end parameters. Here, name consists of extension too. So, the last index of '.' is the first (start) parameter and name.length() means end of the string is the end parameter. So, it is obvious that the extension is filtered.
No comments:
Post a Comment