1

This is not one of those questions for which I dont know a answer but I am trying to find a better more effective way so here I go.

when saving file name in java I am saving it like this

private static final String REPORT_FILE_PREFIX = "ask_ques"; //$NON-NLS-1$
private static final String TXT_EXTENSION = ".txt"; //$NON-NLS-1$
StringBuilder fileName= new StringBuilder();
fileName.append(tempFilePath.toString()).append(REPORT_FILE_PREFIX)
                .append(uniqueId.toString()).append(TXT_EXTENSION);

This is going to save the file something like ask_ques_1234.txt all is well and good. Now when opening the file I need to open in based on its extension like notepad for txt, excel so on and so forth.Like I do below

 if (FilenameUtils.getExtension(fileName.getName()) == CSV_EXTENSION) {
            // Open the .csv file based on the local file type
            // association

        } else if (FilenameUtils.getExtension(fileName.getName()) == TXT_EXTENSION) {
           // open the .txt file based on file type assocaition
}

the problem here is that FilenameUtils.getExtension(reportFile.getName()) returns a value of txt but TXT_EXTENSION has .txt as its value.

Do not want to create static variable for the .. Wanted to see if there is simeple regex or something similar you guys know off to ignore the . when comparing.

Praveen
  • 557
  • 1
  • 5
  • 20
  • 3
    You probably don't want `==` for this. – pvg Dec 22 '16 at 20:47
  • 2
    http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – denvercoder9 Dec 22 '16 at 20:48
  • thanks for the idea @PM77-1 did a slight tweak and it works wonders. if (CSV_EXTENSION.equalsIgnoreCase("." + FilenameUtils.getExtension(reportFile.getName()))) – Praveen Dec 22 '16 at 20:54
  • I suggest having `TXT_EXTENSION = "txt"`and then `...append('.').append(TXT_EXTENSION)` – Martin Milichovsky Dec 22 '16 at 20:55
  • Are you actually trying to open a file based on local file type association, as the comment says? As in, launch the appropriate app for it? Because in that case you don't need to do any of this. – pvg Dec 22 '16 at 22:59
  • care to expand on it I am curious – Praveen Dec 22 '16 at 23:06
  • See http://stackoverflow.com/questions/550329/how-to-open-a-file-with-the-default-associated-program If you're reimplementing that in your own app, you probably shouldn't. – pvg Dec 23 '16 at 00:37

1 Answers1

1

Instead of fileName.getName()) == CSV_EXTENSION use fileName.getName()).equals(CSV_EXTENSION).

In Java == stands for reference comparision and String#equals compares char by char.

To compare csv with .csv you can try the following:

CSV_EXTENSION.substring(1).equals(fileName.getName()))
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • `fileName.getName()` returns extension without the dot. So maybe concat a dot with `fileName.getName()` and then compare it with `CSL_EXTENSIION` – denvercoder9 Dec 22 '16 at 20:49
  • 1
    yep thanks for that also I replaced equals with equalsIgnoreCase for good measure and also flipped the order to prevent null pointer exception – Praveen Dec 22 '16 at 20:56
  • 1
    also like what you did with substring nice idea – Praveen Dec 22 '16 at 21:10