为了清楚起见,我不是在寻找MIME类型.
假设我有以下输入: /path/to/file/foo.txt
我想要一种方法来打破这种输入,特别.txt是扩展.在Java中有没有内置的方法来做到这一点?我想避免编写自己的解析器.
如何获取Array[io.BufferedSource]与给定目录中的通配符匹配的所有文件?
即,如何定义的方法io.Source.fromDir,使得
val txtFiles: Array[io.BufferedSource] = io.Source.fromDir("myDir/*.txt") // ???
Run Code Online (Sandbox Code Playgroud)
从Oathy考试的"Kathy Sierra Bert Bates"一书中我发现了以下代码
public class FileTest {
public static void matches(Path path, String glob){
PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob);
System.out.println(matcher.matches(path));
}
public static void main(String[] args) throws IOException {
Path path = Paths.get("/com/java/One.java");
matches(path, "glob:*.java");
matches(path, "glob:**/*.java");
matches(path, "glob:*");
matches(path, "glob:**");
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
false
true
false
true
Run Code Online (Sandbox Code Playgroud)
我无法清楚地理解输出.有人会解释我吗?让我知道我的例子是什么是跨越目录边界.谢谢洛基
我需要将所有以.txt结尾的文件移动到存档文件夹中。
我有以下代码,但是if (sourcepath.endsWith(".txt"))不验证文件扩展名。
File directory = new File("Archive");
System.out.println((System.getProperty("user.dir")));
File directory1 = new File (System.getProperty("user.dir"));
File[] files = directory1.listFiles();
if (! directory.exists()) {
directory.mkdir();
for(File f : files ) {
Path sourcePath = Paths.get(f.getName());
System.out.println(sourcePath);
if (sourcePath.endsWith(".txt")){ // Not validating
System.out.println(sourcePath.endsWith(extension));
Files.move(sourcePath, Paths.get("Archive"));
}
}
System.out.println("Clearing Folder.....All Files moved to Archive Directory");
}
Run Code Online (Sandbox Code Playgroud)
预期产量:
C:\Users\harsshah\workspace\FFPreBatchValidation
.classpath
.project
.settings
bin
kjnk.txt
src
Run Code Online (Sandbox Code Playgroud)
kjnk.txt应移至存档文件夹