我在尝试为我正在从事的项目创建的程序中遇到了障碍。目前,我需要在本地目录中搜索文件,我看到 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 version "1.7.0_45" Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode )
我不明白我在这里做错了什么。它会是我的 IDE 吗?Mac OSX 有问题吗?我想我可以使用 Java 6 的方式进行文件搜索,但这似乎要容易得多。谢谢大家的帮助!