Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4224436
Votes 3
Synopsis File.{check,get,has}Extension() to access filename extension
Category java:classes_io
Reported Against 1.2.1
Release Fixed
State 3-Accepted, request for enhancement
Priority: 4-Low
Related Bugs 4313887
Submit Date 26-MAR-1999
Description


When dealing with files, it is occasionally necessary to
look at the extension of a file. I think a very useful
potential feature of java.io.File would be some methods
that allow checking of a file extension. Some methods that
would be most useful are:

public String getExtension()
  // returns the file extension, or
  // null if there is not one

public boolean hasExtension()
  // returns true if the file has an
  // extension of the format '.xxx'
  // and false if the file does not
  // have an extension.

public boolean checkExtension( String E )
  // returns true if E (and only E) follows
  // the last period in the filename, returns
  // false if there is no extension or if E
  // doesn't match the extension
(Review ID: 56140) 
======================================================================
Posted Date : 2005-08-25 18:44:34.0
Work Around




Write my own functions - easy to do, but it isn't an effective
use of my time on a programming project.
======================================================================
Evaluation
N/A
Comments
  
  Include a link with my name & email   

Submitted On 06-MAR-2006
valjok
Other functions like math abs, max and sign are also trivial. But the reason for them to include into system libs is His Majesty REUSE. However, I think it is too much to include those has- and get- extension functions into API since they can be implemented inline:

hasExtension = (getExtension() != null)
checkExtension(e) = (getExtension().compareTo(e) == 0)

Does URL api include them? IMO, it is not desirable to clog up the File class by such tiny dust. The getBase method returning the part of the name preceding the extension would be more meaningful. Here are both from my Utils:

	static String getExtension(String s) {
	  int i = s.lastIndexOf('.');
	  return i != -1 ? s.substring(i+1) : null;
	}
	
	static String getBase(String s) {
	  int i = s.lastIndexOf('.');
	  return i != -1 ? s.substring(0, i) : null;
	}


Surprisingly, no IndexBoundException fires; therefore, the JFileChooser demo seems not optimal since checking on string length before calling substring is redundant:

    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;
    }


Submitted On 06-MAR-2006
mlk
Extentions are the wrong way to do this, getMimeType would be better.


Submitted On 07-MAR-2006
jwenting
Sine file extensions are platform dependent (not just the existence but the meaning and delimitation) they don't belong in the core API.
If user wants them he can always create his own system for reading and interpreting them on a per-platform basis.


Submitted On 07-MAR-2006
valjok
The goolge "define:File extension" falls the definition into two cathegories:

1) MS-DOS extension is a 3-byte field in the the FAT file/direrctory record;
2) The character string after the right-most period in a filename.

The 2nd one is universal and it the subject of this RFE, since workaround "Write my own functions - easy to do" does not imply anything more complex than parsing filename.



PLEASE NOTE: JDK6 is formerly known as Project Mustang