如何在Linux Ubuntu上用Java运行递归文件搜索? [英] How do you run a recursive file search in Java on Linux Ubuntu?
问题描述
您好。我正在尝试在Java中运行递归文件搜索,以返回系统上所有.mp3文件的列表。现在,我的背景是在.NET中,我对Linux和Java都很新。我的目标是创建一个能够对我进行搜索的小型跨平台应用程序。在帐户上我需要从网站上启动应用程序,我认为我最好的选择是Java。无论如何,我根据我在线阅读的文章编写了代码。这是我目前的代码:
Hi. I''m trying to run a recursive file search in Java to return a list of all .mp3 files on the system. Now, my background is in .NET, and I''m fairly new to both Linux and Java. My goal is to create a small crossplatform app that is capable of doing this search for me. On the account I need the application to be launched from a website, I figured my best bet was Java. Anyway, I based the code I have now off an article I read online. This is my current code:
startSearch();
List<String> fs = new ArrayList<String>();
public void startSearch()
{
File[] roots = File.listRoots();
for(int i = 0; i < roots.length; i++)
{
File[] files = new File(roots[i].toString()).listFiles();
getFiles(files);
}
JOptionPane.showMessageDialog(null, fs.get(0)); //This is just to test the function...
}
public void getFiles(File[] files)
{
for(File file : files) ***
{
if(file.isDirectory())
{
getFiles(file.listFiles());
}
else
{
fs.add(file.toString());
}
}
}
然而,每当我运行它时,我得到上面一行上的以下错误,标有***:
However, whenever I run it I get the following error on the line above, marked with the ***:
java.lang.NullPointerException
at Form1.getFiles(Form1.java:132)
at Form1.getFiles(Form1.java:136)
at Form1.startSearch(Form1.java:122)
at Form1.initialize(Form1.java:111)
at Form1.<init>(Form1.java:56)
at Form1$1.run(Form1.java:42)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:673)
at java.awt.EventQueue.access$300(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:634)
at java.awt.EventQueue$2.run(EventQueue.java:632)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:643)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
任何想法是什么''错了?我认为它可能与事实有关,唯一的驱动器名称返回''/''并且它不知道在哪里看或什么的。我已经习惯了基于驱动的(C:/)文件系统。任何帮助,将不胜感激。在此先感谢!
Any ideas what''s wrong? I think it might have something to do with the fact the only drive name returned is ''/'' and it doesn''t know where to look or something. I''m so used to drive based (C:/) filesystems. Any help would be appreciated. Thanks in advance!
推荐答案
1.run(Form1.java:42)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java: 226)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:673)
at java.awt.EventQueue.access
1.run(Form1.java:42) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:673) at java.awt.EventQueue.access
300(EventQueue.java: 96)
at java.awt.EventQueue
300(EventQueue.java:96) at java.awt.EventQueue
2.run(EventQueue.java:634)
at java.awt.EventQueue
2.run(EventQueue.java:634) at java.awt.EventQueue
这篇关于如何在Linux Ubuntu上用Java运行递归文件搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!