Skip to content

Instantly share code, notes, and snippets.

@MericBERBER
Created June 6, 2017 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MericBERBER/17f66f6a5261849d1b63360b662238ef to your computer and use it in GitHub Desktop.
Save MericBERBER/17f66f6a5261849d1b63360b662238ef to your computer and use it in GitHub Desktop.
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitOption.*;
import java.util.*;
public class Find {
public static class Finder
extends SimpleFileVisitor<Path> {
private final PathMatcher matcher;
private int numMatches = 0;
Finder(String pattern) {
matcher = FileSystems.getDefault()
.getPathMatcher("glob:" + pattern);
}
//Pattern ile dosyayı karşılaştır
//Eşleşiyorsa yazdır.
void find(Path file) {
Path name = file.getFileName();
if (name != null && matcher.matches(name)) {
numMatches++;
System.out.println(file);
}
}
// Kaç dosya eşleşti?
void done() {
System.out.println("Matched: "
+ numMatches);
}
//Her dosyada
//eşleştirme metodunu çağır.
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) {
find(file);
return FileVisitResult.CONTINUE;
}
// Her directoryde eşleştirme metodunu çağır.
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) {
find(dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file,
IOException exc) {
System.err.println(exc);
return FileVisitResult.CONTINUE;
}
}
public static void main(String[] args)
throws IOException {
Path startingDir = Paths.get("D:/File");
System.out.println("F ile veya TXT ile başlayan dosyalar :");
String pattern ="{F*,TXT*}" ;
Finder finder = new Finder(pattern);
Files.walkFileTree(startingDir, finder);
finder.done();
System.out.println("---------------");
System.out.println("İsminde 0,1,2,3 bulunduran dosyalar :");
String pattern2 ="{*[0-3]*}";
Finder finder2 = new Finder(pattern2);
Files.walkFileTree(startingDir, finder2);
finder2.done();
System.out.println("---------------");
System.out.println("A ile başlayıp devamında en az 1 karakter içeren ve txt ile biten dosyalar: ");
String pattern3 = "A?*.txt";
Finder finder3 = new Finder(pattern3);
Files.walkFileTree(startingDir,finder3);
finder3.done();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment