我正在尝试为我正在进行的项目创建的程序遇到障碍。目前,我需要在本地目录中搜索文件,我看到SDK 7引入了更好的方法来搜索java.nio.File和PathMatcher。不幸的是,我遇到了问题。我设置了一个简单的代码,看看我是否可以为用户输入的任意字符串设置PathMatcher(当然,格式为GLOB)。它如下:
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
import java.io.IOException;
import java.util.*;
import java.nio.file.*;
public class test4{
public static void main(String[] args)
{
String pattern = args[0];
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
}
}
这将返回以下编译时错误:
File: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java [line: 2]
Error: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java:2: package java.nio.file does not exist
File: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java [line: 3]
Error: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java:3: package java.nio.file.attribute does not exist
File: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java [line: 4]
Error: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java:4: package java.nio.file does not exist
File: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java [line: 7]
Error: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java:7: package java.nio.file does not exist
File: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java [line: 13]
Error: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java:13: cannot find symbol
symbol : class PathMatcher
location: class test4
File: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java [line: 13]
Error: /Users/xenosphobos/Documents/Thesis/ThesisJava/test4.java:13: cannot find symbol
symbol : variable FileSystems
location: class test4
我目前正在运行MAC OSX 10.8.4并安装了SDK 7.45。我使用IDE DrJava(我知道,Eclipse更好)。
当我运行java -version时,我得到: java版“1.7.0_45” Java(TM)SE运行时环境(版本1.7.0_45-b18) Java HotSpot(TM)64位服务器VM(内置24.45-b08,混合模式)
我不明白我在这里做错了什么。它可能是我的IDE吗? Mac OSX有问题吗?我想我可以使用Java 6的方式进行文件搜索,但这似乎更容易。谢谢大家的帮助!