在java中查找特定命名方案的所有文件
假设我有一堆这样命名的文件:
10011-1-chassis.EDRW
10011-2-front.EDRW
10011-3-rear.EDRW
20011-1-chassis.EDRW
20011-2-front.EDRW
20011-3-back.EASM
20011-3-cover.EASM
如果我只想要 20011-x 文件,那么我将如何列出所有这些文件,然后只列出适当的文件,以便我可以在 JOptionPane 中显示文件名,供用户从他们对哪个文件感兴趣?
Suppose I have a bunch of files named as such:
10011-1-chassis.EDRW
10011-2-front.EDRW
10011-3-rear.EDRW
20011-1-chassis.EDRW
20011-2-front.EDRW
20011-3-back.EASM
20011-3-cover.EASM
If I want only the 20011-x files then how would I list them all and then only the appropriate files so that I could present the filenames in a JOptionPane for the user to choose from a dropdown which file they were interested in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编写一个 java.io.FilenameFilter ,它使用正则表达式过滤掉不可接受的文件名。
我推荐使用正则表达式,因为我假设您想要动态更改模式。在这种情况下,这并不过分。您引用的示例的硬连线解决方案对我来说似乎没有那么有用。我假设用户希望通过在用户界面中指定模式来告诉您他们希望如何更改模式。
Write a
java.io.FilenameFilter
that uses a regular expression that filters out unacceptable file names.I recommend a regex because I'm assuming that you'll want to change the pattern dynamically. It's not overkill in that case. A hard-wired solution for the example you cite doesn't seem that useful to me. I'm assuming that users will want to tell you how they want to change the pattern by specifying it in your UI..
使用
JFileChooser
而不是带有列表的JOptionPane
。例如Use a
JFileChooser
instead of theJOptionPane
with list. E.G.使用
filename.startsWith ("20011-")
作为过滤谓词。Use
filename.startsWith ("20011-")
as your filtering predicate.