Johnathan Au
Johnathan Au

Reputation: 5362

Is there a way to read the mime types file with Java Properties?

I'm trying to read the mime types file with Java Properties to get the appropriate mime type according to a files extension. However, the file is laid out such that I can only get the file extension for a mime type not the other way round.

For example:

text/html                   html htm

as opposed to the desired format:

html htm                    text/html

Therefore, if I read it with a Properties object like so:

mimeTypes.getProperty("text/html");

I would get html and htm but if I do it the other round:

mimeTypes.getProperty("html");

I would not get text/html which is what I want. So, is there anyway to get the key by providing the key's value?

Thanks for any help and sorry if question is out of order or anything.

Cheers

EDIT:

No, I will not be using this:

FileNameMap fileNameMap = URLConnection.getFileNameMap();
String mimeType = fileNameMap.getContentTypeFor(filename);  

I want to do it the long way because I'm hardcore like that :)

Upvotes: 0

Views: 673

Answers (2)

Brian Pipa
Brian Pipa

Reputation: 806

It sounds like you want to read in the mimetypes file, parse it, and put it into a "reversed" HashMap of extension:type (rather than type:extensions). You could still read it in with Properties, just once you have it, you would then need to parse it into the reversed HashMap. I think this would make your code much cleaner.

Upvotes: 0

FordFulkerson
FordFulkerson

Reputation: 140

you can use the entrySet() function to get the Mappings (key,value). Then iterate over that set. I don't think there is a direct method in the collections api to get a key from value.

Upvotes: 1

Related Questions