From 68efc348bdd9457b28035a4702ef4480336c0342 Mon Sep 17 00:00:00 2001 From: coolneng Date: Sun, 25 Oct 2020 20:24:59 +0100 Subject: [PATCH] Read all files from a directory --- src/main/java/org/RI/P1/TODO.org => TODO.org | 0 pom.xml | 4 +-- src/main/java/org/RI/P1/AnalyzeDirectory.java | 32 +++++++++++++++++++ src/main/java/org/RI/P1/FileData.java | 13 ++------ 4 files changed, 36 insertions(+), 13 deletions(-) rename src/main/java/org/RI/P1/TODO.org => TODO.org (100%) create mode 100644 src/main/java/org/RI/P1/AnalyzeDirectory.java diff --git a/src/main/java/org/RI/P1/TODO.org b/TODO.org similarity index 100% rename from src/main/java/org/RI/P1/TODO.org rename to TODO.org diff --git a/pom.xml b/pom.xml index e6a0303..5e39ac8 100644 --- a/pom.xml +++ b/pom.xml @@ -14,8 +14,8 @@ UTF-8 - 1.7 - 1.7 + 1.8 + 1.8 diff --git a/src/main/java/org/RI/P1/AnalyzeDirectory.java b/src/main/java/org/RI/P1/AnalyzeDirectory.java new file mode 100644 index 0000000..f11e598 --- /dev/null +++ b/src/main/java/org/RI/P1/AnalyzeDirectory.java @@ -0,0 +1,32 @@ +package org.RI.P1; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.tika.exception.TikaException; +import org.xml.sax.SAXException; + +public class AnalyzeDirectory { + private static List files; + + private static void readFiles(String directory) throws IOException { + files = Files.walk(Paths.get(directory)).filter(Files::isRegularFile).map(Path::toFile) + .collect(Collectors.toList()); + } + + public static void main(String[] args) throws IOException, TikaException, SAXException { + String directory = args[0]; + readFiles(directory); + for (File file : files) { + FileData data = new FileData(file); + System.out.println(data); + System.out.println("--------------------------------------------------------------------------"); + } + } + +} diff --git a/src/main/java/org/RI/P1/FileData.java b/src/main/java/org/RI/P1/FileData.java index 4b33451..229e590 100644 --- a/src/main/java/org/RI/P1/FileData.java +++ b/src/main/java/org/RI/P1/FileData.java @@ -30,13 +30,14 @@ public class FileData { FileData() { } - FileData(File file) throws FileNotFoundException { + FileData(File file) throws IOException, TikaException, SAXException { inputStream = new FileInputStream(file); metadata = new Metadata(); parser = new AutoDetectParser(); contentHandler = new BodyContentHandler(-1); parseContext = new ParseContext(); langIdentifier = new OptimaizeLangDetector().loadModels(); + setMetadata(); } private void setMetadata() throws IOException, TikaException, SAXException { @@ -53,14 +54,4 @@ public class FileData { + language.getLanguage() + "\n"; } - public static void main(String[] args) throws IOException, TikaException, SAXException { - try { - File file = new File(args[0]); - FileData data = new FileData(file); - data.setMetadata(); - System.out.println(data); - } catch (FileNotFoundException exp) { - System.out.println("The file " + args[0] + " could not be found"); - } - } }