Minor Issue when saving vs retrieving file extension in java

Praveen

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.

xenteros

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()))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Minor issue when working with extended classes in Java

From Dev

Minor issue when working with extended classes in Java

From Dev

Is there a new Java 8 way of retrieving the file extension?

From Dev

issue with saving doc file

From Dev

Django: When saving issue

From Dev

Java saving file under same name in same directory with different extension

From Dev

nhibernate : saving and retrieving a file to/from database

From Dev

Minor Issue when doing method overriding in SONAR

From Dev

Saving a file from Chrome Extension

From Dev

from DataGridView saving to text file issue when saving date values on vb.net

From Dev

What is the best practice for saving and retrieving a java object?

From Dev

File extension issue

From Dev

Checking if file exists when saving file in Java Swing

From Dev

Regular Expression for retrieving File Extension in HTTP url

From Dev

Regular Expression for retrieving File Extension in HTTP url

From Dev

How to set the directory when saving file with filewriter in java?

From Dev

saving data to a file in Java

From Dev

Extension not added to file name after saving

From Dev

How to make minor edits to an XML file in Java

From Dev

TSaveDialog file extension and [ofOverwritePromt] issue

From Dev

Missing extension when saving image from canvas

From Dev

Missing extension when saving image from canvas

From Dev

Java - Issue retrieving value from hashtable

From Dev

Java - Issue retrieving value from hashtable

From Dev

Does RichEditBox text need to be encoded/decoded when saving/retrieving? (UWP)

From Dev

File saving issue PYTHON - duplicate files?

From Dev

Assuredly simple C# file saving issue

From Dev

Matlab Issue with saving figure to an image file

From Dev

Matlab Issue with saving figure to an image file

Related Related

  1. 1

    Minor issue when working with extended classes in Java

  2. 2

    Minor issue when working with extended classes in Java

  3. 3

    Is there a new Java 8 way of retrieving the file extension?

  4. 4

    issue with saving doc file

  5. 5

    Django: When saving issue

  6. 6

    Java saving file under same name in same directory with different extension

  7. 7

    nhibernate : saving and retrieving a file to/from database

  8. 8

    Minor Issue when doing method overriding in SONAR

  9. 9

    Saving a file from Chrome Extension

  10. 10

    from DataGridView saving to text file issue when saving date values on vb.net

  11. 11

    What is the best practice for saving and retrieving a java object?

  12. 12

    File extension issue

  13. 13

    Checking if file exists when saving file in Java Swing

  14. 14

    Regular Expression for retrieving File Extension in HTTP url

  15. 15

    Regular Expression for retrieving File Extension in HTTP url

  16. 16

    How to set the directory when saving file with filewriter in java?

  17. 17

    saving data to a file in Java

  18. 18

    Extension not added to file name after saving

  19. 19

    How to make minor edits to an XML file in Java

  20. 20

    TSaveDialog file extension and [ofOverwritePromt] issue

  21. 21

    Missing extension when saving image from canvas

  22. 22

    Missing extension when saving image from canvas

  23. 23

    Java - Issue retrieving value from hashtable

  24. 24

    Java - Issue retrieving value from hashtable

  25. 25

    Does RichEditBox text need to be encoded/decoded when saving/retrieving? (UWP)

  26. 26

    File saving issue PYTHON - duplicate files?

  27. 27

    Assuredly simple C# file saving issue

  28. 28

    Matlab Issue with saving figure to an image file

  29. 29

    Matlab Issue with saving figure to an image file

HotTag

Archive