Java JFileChooser getAbsoluteFile Add File Extension

This problem works for me, but I would like to know if there is a better way to add the file extension?

what am i doing right now:

String filePath = chooser.getSelectedFile().getAbsoluteFile() + ".html";

im adding an extension is hardcoded. and then save it.

just wondering if there is a more reliable / logical way that can be implemented?

Thank you for your time.

EDIT: I am asking about this since I want my application to be portable across platforms. so by adding .html manually, I can do this only for Windows.

EDIT: I think ive is surfing enough to know that .html is hard-coded, safe, as I have not found any documentation that says don't use this approach (not quite sure).

ISSUE: also, if I want to save the file in a different format, for example, how to determine that the user has chosen which format?

FileNameExtensionFilter can add filters to the dialog, but how to get the return value for the selected file type?

EDIT: I studied this , but it is still unclear how to extract the user selected file type.

EDIT: this is a rephrase of my problem:

alt text http://img98.imageshack.us/img98/4904/savef.jpg my question is: how can I get / find out which of the two filters the user has selected as the save format. HTML or JPEG? How can I get this information from JFileChooser? thank.

EDIT: found something: this has something to do with JFileChooser.getFileFilter () Your help is still appreciated.

EDIT: getFileFilter() FileNameExtensionFilter .

+3
3

, :

JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
chooser.setAcceptAllFileFilterUsed(false);

FileNameExtensionFilter filter = new FileNameExtensionFilter("HTML Documents", "htm", "html");
chooser.setFileFilter(filter);

int option = chooser.showSaveDialog(ChatGUI.this);
if (option == JFileChooser.APPROVE_OPTION) {
    // Set up document to be parsed as HTML
    StyledDocument doc = (StyledDocument)textPaneHistory.getDocument();
    HTMLEditorKit kit = new HTMLEditorKit();

    BufferedOutputStream out;

    try {
        System.out.println(chooser.getFileFilter());

        if (chooser.getFileFilter() == filter)
            System.out.println("ha ha");
    }
}
+2

, :

, FileFilter FileNameExtensionFilter, getExtensions().

JFileChooser fileChooser = new JFileChooser("");

// Prevent user to use the default All Files option
fileChooser.setAcceptAllFileFilterUsed(false);

[...]

// Get the FileFilter
FileFilter ff = fileChooser.getFileFilter();

// Cast the FileFilter to FileNameExtensionFilter
FileNameExtensionFilter extFilter = (FileNameExtensionFilter)ff;

// Get the Extension
String ext = extFilter.getExtensions()[0];

:

ext = ((FileNameExtensionFilter)fileChooser.getFileFilter()).getExtensions()[0];
+1

, . , ? , . , "/Users/banang/Documents/anything.html" , anything.html.

JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);
System.err.println(chooser.getSelectedFile().getCanonicalPath());

, .

0

Source: https://habr.com/ru/post/1743189/


All Articles