I'm trying to detect if the file that I am opening is a HTML file.

I have already tried this:

try {
   String file = fileName.getCanonicalPath();

   if (file.endsWith(".htm") | file.endsWith(".html")) {

   }

   } catch (IOException e) {
       e.printStackTrace();
   }

But the file.endsWith(); doesn't seem to be detecting anything. The fileName is the file that I'm opening. Let me know if I have to post the code that I use to open the file.

Thanks in advance.

有帮助吗?

解决方案

This line looks suspect:

if (file.endsWith(".htm") | file.endsWith(".html")) {

The | operator is the bitwise-OR operator. You need the logical-OR operator, ||.

EDIT

Adding what @MadProgrammer suggessted in a comment:

Call toLowerCase() on the filename to account for the possibility that the file ends in .HTM or .HTML.

其他提示

You might have a case issue here as well. As @rgettman points out you probably need a logical or in there too

if (file.toLowerCase().endsWith(".htm") || file.toLowerCase().endsWith(".html")) {
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top