如何在javafx中获取文件的扩展名?
我想将如何在javafx中获取文件的扩展名?,javafx,javafx-2,javafx-8,Javafx,Javafx 2,Javafx 8,我想将.c文件拖到我的javaFx文本区域, 因此,对于地址,可以将其视为字符串,然后使用“endswith(“.c”)”方法检查其扩展名,但如果它是一个文件,我不知道如何操作。您可以从文件对象获取扩展名: String fileExtension = file.getName().substring( file.getName().lastIndexOf(".") + 1); 加上通常的null检查、索引越界等。从目录中获取所有文件名,然后使用此代码 File path = new
.c
文件拖到我的javaFx文本区域,
因此,对于地址,可以将其视为字符串,然后使用“endswith(“.c”)”方法检查其扩展名,但如果它是一个文件,我不知道如何操作。您可以从
文件对象获取扩展名:
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());
File path=新文件(“D:+File.separator+“MyDownloads”);
//指定您的目录
File[]files=path.listFiles();
StringBuffer cFiles=新的StringBuffer();
对于(int i=0;i
cFile将为您提供扩展名为“c”的文件名,然后在您的文本区域中设置该文本