Java 找不到符号文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19183607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot find symbol File
提问by user2846960
I'm working on a course project and using this code block my professor gave us to, one, get all files from the current directory and, two, to find which files are in the .dat format. Here is the code block:
我正在处理一个课程项目,并使用我的教授给我们的这个代码块,一是从当前目录中获取所有文件,二是查找哪些文件是 .dat 格式。这是代码块:
// Get all files from directory
File curDir = new File(".");
String[] fileNames = curDir.list();
ArrayList<String> data = new ArrayList<String>();
// Find files which may have data. (aka, are in the .dat format)
for (String s:fileNames)
if (s.endsWith(".dat"))
data.add(s);
However, when I try to compile and test my program, I get this error message in response:
但是,当我尝试编译和测试我的程序时,我收到此错误消息作为响应:
Prog2.java:11: cannot find symbol
symbol : class File
location: class Prog2
File curDir = new File(".");
^
Prog2.java:11: cannot find symbol
symbol : class File
location: class Prog2
File curDir = new File(".");
^
I admittedly have minimal experience with the File
class, so it might be my fault entirely, but what's up with this?
诚然File
,我对这门课的经验很少,所以这可能完全是我的错,但这是怎么回事?
回答by Naveen Kumar Alonekar
Import the File
class from the java.io.File
package
File
从java.io.File
包中导入类
i.e.
IE
import java.io.File;
Hereis documentation for java.io.File
and a brief explanation of the File
class.
回答by Konstantin Yovkov
Just add the following statement before the class definition:
只需在类定义之前添加以下语句:
import java.io.File;
If you use IDE, like Eclipse, JDeveloper, NetBeans, etc. it can automatilly add the import
statement for you.
如果您使用 IDE,如 Eclipse、JDeveloper、NetBeans 等,它可以自动import
为您添加语句。
回答by AnxGotta
I think Naveen and Poodle have it right with the need to import the File class
我认为 Naveen 和 Poodle 是正确的,需要导入 File 类
import java.io.File;
Here is another method of getting .dat files that helped me, just FYI =)
这是获取 .dat 文件的另一种方法,对我有帮助,仅供参考 =)
It's a general file filtering method that works nicely:
这是一种效果很好的通用文件过滤方法:
String[] fileList;
File mPath = new File("YOUR_DIRECTORY");
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return filename.contains(".dat");
// you can add multiple conditions for the filer here
}
};
fileList = mPath.list(filter);
if (fileList == null) {
//handle no files of type .dat
}
As I said in the comments, you can add multiple conditions to the filter to get specific files. Again, just FYI.
正如我在评论中所说,您可以向过滤器添加多个条件以获取特定文件。再次,仅供参考。