如何在 javafx 中获取文件拖动文件的扩展名?

how to get extension of file dragged file in javafx?

我想将 .c 个文件拖到我的 javaFx TextArea 中, 因此,如果是地址,可以将其视为一个字符串,然后使用 "endswith(".c")" 方法检查它的扩展名,但如果它是一个文件[=,我不知道该怎么做11=]

您可以从 File 对象中获取扩展名:

String fileExtension = file.getName().substring( file.getName().lastIndexOf(".") + 1);

加上常规的 null、索引越界等检查

从目录中获取所有文件名,然后使用此代码。

    File path = new File("D:" + File.separator + "MyDownloads");
    // Specify your directory
    File[] files = path.listFiles();
    StringBuffer cFiles = new StringBuffer();
    for (int i = 0; i < files.length; i++) {
        System.out.println(files[i].getName());
        if (files[i].getName().endsWith(".c")) {
            cFiles.append(files[i].getName() + "\n");
        }
    }
    // Give your text area variable in the place of textArea.
    textArea.setText(cFiles.toString());

cFile 将为您提供扩展名为 "c" 的文件名,然后在您的 TextArea 中设置该文本。