0

I had a requirement like, i want to test the file extension of the fields starting from index 10 in the following code:

Sunset.jpg;REF;Title;DESC;10;1;11;21;31;Bluehills.jpg;Winter.ppt;Moonfit.xls...

In the above code i want to read the extesnsion of the fields starting from inedx 10(Bluehills.jpg and soon), to check whether it is valid extension or not.

Any help is highly appreciated.

Thanks,

Raj

user27
  • 274
  • 9
  • 26
  • What have you tried? Do you know how to get array or list elements by index, and how to use regular expressions? – Thilo Apr 22 '12 at 02:05
  • Hi @Thilo i have tried using CSV Reader..i got to read the fields but how to get the fieds extension particularly startig from an index as i mentioned above – user27 Apr 22 '12 at 02:10
  • possible duplicate of [Java: splitting the filename into a base and extension](http://stackoverflow.com/questions/4545937/java-splitting-the-filename-into-a-base-and-extension) – Thilo Apr 22 '12 at 02:11

1 Answers1

0
  • Read the file and parse the line(s) as String
  • Make a substring from index n to string.length().
  • Split the the file names in a String ArrayList using the split() function with ";" as delimiter.
  • Use the following function to retrieve the file extension

    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');
    
        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
     }
    
Chris911
  • 4,131
  • 1
  • 23
  • 33