Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
/**
*
*/
package de.tutorials;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
import static de.tutorials.SearchTextInFilesExample.SearchTarget.*;
/**
* @author Tom
*
*/
public class SearchTextInFilesExample {
enum SearchTarget{
FILES,
FOLDERS{
public boolean isTarget(File file){
return file != null && file.isDirectory();
}
},
IMAGES{
public boolean isTarget(File file){
return file.isDirectory() || file.getName().endsWith(".jpg");
}
};
public boolean isTarget(File file){
return file != null;
}
}
/**
* @param args
*/
public static void main(String[] args) {
for (File file : new FileFinder().find(FILES).beneath("c:/temp")
.recursively().containing("1234").named(".*\\.txt")
.result()) {
System.out.println(file);
}
}
static class FileFinder {
boolean recursively;
String basePath;
FileFilter fileFilter;
String searchPattern;
String searchString;
SearchTarget searchTarget;
public FileFinder containingPattern(String searchPattern) {
this.searchPattern = searchPattern;
return this;
}
public FileFinder containing(String string) {
this.searchString = string;
return this;
}
public Iterable<File> result() {
File base = new File(basePath);
List<File> results = new ArrayList<File>();
Stack<File> stack = new Stack<File>();
stack.push(base);
while (!stack.empty()) {
File currentFile = stack.pop();
if (currentFile != null) {
if (currentFile.isFile()
&& matchesSearchCriterias(currentFile)) {
results.add(currentFile);
}
if (currentFile.isDirectory()) {
for (File file : currentFile.listFiles(fileFilter)) {
stack.add(file);
}
}
}
}
return results;
}
private boolean matchesSearchCriterias(File file) {
boolean matches = false;
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (searchPattern != null) {
matches = line.matches(searchPattern);
} else if (searchString != null) {
matches = line.contains(searchString);
}
if (matches) {
break;
}
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return matches;
}
public FileFinder named(final String namePattern) {
fileFilter = new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory()
|| pathname.getName().matches(namePattern);
}
};
return this;
}
public FileFinder recursively() {
recursively = true;
return this;
}
public FileFinder beneath(String path) {
this.basePath = path;
return this;
}
public FileFinder find(SearchTarget searchTarget) {
this.searchTarget = searchTarget;
return this;
}
}
}
....containing("1234")...