0

I have the code to get the mime type. It seems to work. When I put mimeType in an if statement, I don't get the desired effect. Here are the snippets.

GET the mime type--

MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String mimeType = mimeTypesMap.getContentType(file);

Now I want to only open a file if the mime type is text--

if (file.isFile()) {
try {
    if (mimeType == "text/plain") openFile(file);
} catch (IOException e) {
    e.printStackTrace();
        System.err.print("    Cannot process file....\n");
}
}

This will not open a text file. Any ideas on why it will not work? openFile(file) works and is not the problem. file is a File Object. Thanks.

apaderno
  • 28,547
  • 16
  • 75
  • 90

2 Answers2

1

You're comparing the MIME type using ==, not String#equals() -- it's a String, yes? You probably have to do this:

if (mimeType.equals("text/plain")) openFile(file);

Or better still:

if ("text/plain".equals(mimeType)) openFile(file);

since that prevents a NPE from getting in your way.

delfuego
  • 14,085
  • 4
  • 39
  • 39
  • Wow. How dumb am I. I knew that. Been doing some coding in Perl and JS and I did not reset my thinking. Thanks for clearing that up with me. – David Hoffman Dec 21 '09 at 06:12
0

Maybe the mime type is just text?

MIME types file search order:

The MimetypesFileTypeMap looks in various places in the user's system for MIME types file entries. When requests are made to search for MIME types in the MimetypesFileTypeMap, it searches MIME types files in the following order:

  1. Programmatically added entries to the MimetypesFileTypeMap instance.
  2. The file .mime.types in the user's home directory.
  3. The file /lib/mime.types.
  4. The file or resource named META-INF/mime.types.
  5. The file or resource named META-INF/mimetypes.default (usually found only in the activation.jar file).

Find this file and find out what mime type was specified for .txt files. Source: link

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
  • Unlikely. For one, `text` isn't a MIME type. For another, the case for most users is that the fifth item on that list is the one that'll win -- that's the file that's part of the J2EE distribution, and like all MIME type maps, `text/plain` is the type associated with commonly-used text file suffixes. – delfuego Dec 21 '09 at 03:28
  • The answer you give is not helping me at all. I printed out the mimeType variable and got a mime type for everything I clicked on. (I created a tree using a DefaultMutableTreeNode object and a mouse listener. The JTree code is working.) The String that is printed is text/plain. I can give you the complete code, but it is quite large and my problem is only in this method-- public void valueChanged(TreeSelectionEvent event) – David Hoffman Dec 21 '09 at 03:31
  • which I override and is the mouse listener for the JTree. I create a JTree, populate it with DefaultMutableTreeNode's, and when I click on a text file, I want to open it up in a TextArea. I only want to open text files, hence the mime type check. The mime type is being returned properly, I just can't make the file I click on only open if the mime type is the text/plain type. Yes I do check if it is a file and not a directory. Have I explained my dilemma more clearly now? Thanks for your help. – David Hoffman Dec 21 '09 at 03:32
  • David, did you see my answer above? You're not doing your String comparison properly. http://stackoverflow.com/questions/1937903/i-need-to-open-a-file-based-on-its-mime-type/1937993#1937993 – delfuego Dec 21 '09 at 03:40
  • I don't see your point. I am not testing for "text", I am testing for "text/plain", which is what should work, right? It doesn't. I am guessing I can't do that String comparison for some reason. – David Hoffman Dec 21 '09 at 03:59