문제

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