我使用 JFileChoose
选择音乐文件,我想添加我在框架上选择的内容(在JTextArea
中)在这种情况下,我想列出我选择的名称和时间。如何列出所有这些?我做的事是正确的(jtextarea,jfilechoose)吗?
您可以基于扩展名实现Custome文件过滤,以在特定目录中获取音乐文件名。
class CustomeFilter implements FilenameFilter {
@Override
public boolean accept(File directory, String fileName) {
boolean valid = false;
//if (fileName.startsWith("j") )
if ( fileName.endsWith(".mp3") || fileName.endsWith(".wav")) {
valid = true;
} else {
valid = false;
}
return valid;
}
}
实施
File file = new File("C:\My Music");
File[] contens = file.listFiles(new CustomeFilter());
for (File contens : conten) {
System.out.println(conten);
}
然后将列表放在您想要的任何摆动组件上。
但是,如果您从 jfilechooser 中获取文件,则
public File getCurrentFileInformation() {
// display file dialog
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(this);
// if Cancel button on dialog clicked, return
if (result == JFileChooser.CANCEL_OPTION)
System.exit(1);
File selectedFileName = fileChooser.getSelectedFile(); // get selected file
if ((selectedFileName == null) || (selectedFileName.getName().equals(""))) {
JOptionPane.showMessageDialog(this, "Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE);
System.exit(1);
} // end if
return selectedFileName;
}
因此,您可以使用以下方式获得所选的文件信息:
File fileInfo = getCurrentFileInformation();
喜欢 fileInfo.lastModified()
或 fileInfo.length()