Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package task;
- import java.io.File;
- public interface Checker {
- boolean check(File file);
- }
- ------------
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintStream;
- import java.nio.file.FileSystems;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.Executor;
- import java.util.concurrent.Executors;
- import java.util.concurrent.LinkedBlockingQueue;
- import com.ibm.icu.text.CharsetDetector;
- public class FileScan implements Runnable {
- private String whatFind;
- private BlockingQueue<File> queue;
- private CountDownLatch latch;
- private File endOfWorkFile;
- public FileScan(String whatFind, BlockingQueue<File> queue,
- CountDownLatch latch, File endOfWorkFile) {
- this.whatFind = whatFind;
- this.queue = queue;
- this.latch = latch;
- this.endOfWorkFile = endOfWorkFile;
- }
- public FileScan() {
- }
- @Override
- public void run() {
- while (true) {
- try {
- File file;
- file = queue.take();
- if (file == endOfWorkFile) {
- break;
- }
- scan(file);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- latch.countDown();
- }
- private void scan(File file) {
- Scanner scanner = null;
- int matches = 0;
- try {
- scanner = new Scanner(file);
- } catch (FileNotFoundException e) {
- System.out.println("File Not Found.");
- e.printStackTrace();
- }
- while (scanner.hasNext())
- if (scanner.next().equals(whatFind)) {
- matches++;
- }
- if (matches > 0) {
- String myStr = String.format(
- "File: %s - and the number of matches " + "is: %d",
- file.getAbsolutePath(), matches);
- PrintStream ps = new PrintStream(System.out, true);
- ps.println(myStr);
- }
- }
- private void askUserPathAndWord() {
- try {
- tryToAskUserPathAndWord();
- } catch (IOException | RuntimeException e) {
- System.out.println("Wrong input!");
- e.printStackTrace();
- } catch (InterruptedException e) {
- System.out.println("Interrupted.");
- e.printStackTrace();
- }
- }
- private void tryToAskUserPathAndWord() throws IOException,
- InterruptedException {
- PathAndWord pathAndWord = readPathAndWord();
- if (pathAndWord.isProperlyInitialized()) {
- performScan(pathAndWord, "GameOver.tmp");
- System.out.println("Thank you!");
- } else {
- System.out.println("You did not enter anything");
- }
- }
- private PathAndWord readPathAndWord() throws IOException {
- System.out
- .println("Please, enter a Path and Word (which you want to find):");
- BufferedReader bufferedReader = new BufferedReader(
- new InputStreamReader(System.in));
- String path = readPath(bufferedReader);
- String whatFind = readWord(bufferedReader);
- return new PathAndWord(path, whatFind);
- }
- private String readPath(BufferedReader bufferedReader) throws IOException {
- boolean ok = false;
- File pathInput;
- do {
- System.out.println("Please enter a Path:");
- pathInput = new File(bufferedReader.readLine());
- if (pathInput.exists() && pathInput.isDirectory()) {
- ok = true;
- } else {
- System.out.println("Doesn't exist or isn't folder!");
- }
- } while (!ok);
- return pathInput.getAbsolutePath();
- }
- private String readWord(BufferedReader bufferedReader) throws IOException {
- System.out.println("Please enter a Word:");
- return bufferedReader.readLine();
- }
- private void performScan(PathAndWord pathAndWord, String endOfWorkFileName)
- throws InterruptedException {
- BlockingQueue<File> queue = new LinkedBlockingQueue<File>();
- File endOfWorkFile = new File(endOfWorkFileName);
- CountDownLatch latch = new CountDownLatch(2);
- FolderScan folderScan = new FolderScan(pathAndWord.path, queue, latch,
- endOfWorkFile);
- FileScan fileScan = new FileScan(pathAndWord.whatFind, queue, latch,
- endOfWorkFile);
- Executor executor = Executors.newCachedThreadPool();
- executor.execute(folderScan);
- executor.execute(fileScan);
- latch.await();
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- long startTime = System.currentTimeMillis();
- new FileScan().askUserPathAndWord();
- long stopTime = System.currentTimeMillis();
- long elapsedTime = stopTime - startTime;
- System.out.println("\nRuntime time " + elapsedTime + " milliseconds.");
- }
- }
- class FolderScan implements Runnable {
- FolderScan(String path, BlockingQueue<File> queue, CountDownLatch latch,
- File endOfWorkFile) {
- this.path = path;
- this.queue = queue;
- this.latch = latch;
- this.endOfWorkFile = endOfWorkFile;
- checkers = new ArrayList<Checker>(Arrays.asList(
- new ExtentionChecking(), new ProbeContentTypeCheking(),
- new EncodingChecking()));
- List<UncriticalChecker> checkers = Collections
- .singletonList(new UncriticalChecker());
- FolderScan folderScan = new FolderScan(path, queue, latch,
- endOfWorkFile, checkers);
- }
- public FolderScan(String path2, BlockingQueue<File> queue2,
- CountDownLatch latch2, File endOfWorkFile2,
- List<UncriticalChecker> checkers2) {
- }
- @Override
- public void run() {
- try {
- findFiles(path);
- queue.put(endOfWorkFile);
- latch.countDown();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- private void findFiles(String path) {
- try {
- File root = new File(path);
- File[] list = root.listFiles();
- for (File currentFile : list) {
- boolean checksPassed = true;
- if (currentFile.isDirectory()) {
- findFiles(currentFile.getAbsolutePath());
- } else {
- for (Checker currentChecker : checkers) {
- if (!currentChecker.check(currentFile)) {
- checksPassed = false;
- break;
- }
- }
- if (checksPassed) {
- queue.put(currentFile);
- }
- }
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- private String path;
- private BlockingQueue<File> queue;
- private CountDownLatch latch;
- private File endOfWorkFile;
- private List<Checker> checkers;
- private List<UncriticalChecker> checkList;
- }
- class UncriticalChecker implements Checker {
- @Override
- public boolean check(File currentFile) {
- return true;
- }
- }
- class PathAndWord {
- final String path;
- final String whatFind;
- PathAndWord(String path, String whatFind) {
- this.path = path;
- this.whatFind = whatFind;
- }
- boolean isProperlyInitialized() {
- return path != null && whatFind != null;
- }
- }
- class ExtentionChecking implements Checker {
- @Override
- public boolean check(File currentFile) {
- fileName = currentFile.getName().toLowerCase();
- Set<String> extensions = new HashSet<String>(Arrays.asList(".txt",
- ".pdf", ".doc", ".docx", ".html", ".htm", ".xml", ".djvu",
- ".djv", ".rar", ".rtf", ".tmp"));
- if (extensions.contains(fileName.substring(fileName.lastIndexOf(".")))) {
- return true;
- }
- return false;
- }
- private String fileName;
- }
- class EncodingChecking implements Checker {
- @Override
- public boolean check(File currentFile) {
- return detectEncoding(currentFile);
- }
- public static boolean detectEncoding(File file) {
- detector = new CharsetDetector();
- // validate input
- if (null == file) {
- throw new IllegalArgumentException("input file can't be null");
- }
- if (file.isDirectory()) {
- throw new IllegalArgumentException(
- "input file refers to a directory");
- }
- // read input file
- byte[] buffer;
- try {
- buffer = readUTFHeaderBytes(file);
- } catch (IOException e) {
- throw new IllegalArgumentException(
- "Can't read input file, error = " + e.getLocalizedMessage());
- }
- if (detector.setText(buffer) != null) {
- return true;
- }
- return false;
- }
- private static byte[] readUTFHeaderBytes(File input) throws IOException {
- // read data
- FileInputStream fileInputStream = new FileInputStream(input);
- try {
- byte firstBytes[] = new byte[50];
- int count = fileInputStream.read(firstBytes);
- if (count < 5) {
- throw new IOException("Poor file!");
- }
- return firstBytes;
- } finally {
- fileInputStream.close();
- }
- }
- private static CharsetDetector detector;
- }
- class ProbeContentTypeCheking implements Checker {
- @Override
- public boolean check(File currentFile) {
- try {
- Path filePath = FileSystems.getDefault().getPath(
- currentFile.getAbsolutePath());
- if ((null != Files.probeContentType(filePath))) {
- return true;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement