1

First of all, i am a newbie, i'm doing my best to learn and evolve and i'm taking all of your answers seriously.

So esentially, what i have to do is: I'm reading multiple files from multiple folders having a specified encoder (ISO-8859-1) and i have to output a information that i will find being guided by some keywords. For example: I got 300 files and I need to search for random words that are placed only after the keyword ENTER and start with XX, let's say. So if i found 180/300 files that contain this information, i have to output those words somewhere.

I'm posting below the code i've worked on for reading all those files line by line. I think part of what i need is a logger and also something called string tokenizer if i am not mistaken. I think i got a starting point for that logger(i'm posting it aswell) but i don't know how can i apply that for my multiple files and also, how to use the string tokenizer. Any help is appreciated, thank you!

Starting point for logger:

public static class Logs {
static FileHandler fileTxt;
static SimpleFormatter formatterTxt;

static public void logging() throws IOException {

    Logger logger = Logger.getLogger(ListFiles.class.getName());
    logger.setLevel(Level.ALL);
    fileTxt = new FileHandler("C:\\Users\\TG\\eclipse-
workspace\\ListFiles\\src\\file.txt");
    formatterTxt = new SimpleFormatter();
    fileTxt.setFormatter(formatterTxt);
    logger.addHandler(fileTxt);
    logger.info("Random line for testing purposes");

}
}

Code:

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.stream.Stream;


    public class ListFiles {
        public static void main(String[] args) {
           ListFiles listFiles = new ListFiles();
           listFiles.listAllFiles("C:\\Users\\TG\\Desktop\\Q");

 }

   public void listAllFiles(String path){
       try(Stream<Path> paths = Files.walk(Paths.get(path))) {
           paths.forEach(filePath -> {
              if (Files.isRegularFile(filePath)) {
                 try {
                    readContent(filePath);
                      } catch (Exception e) {
                // TODO Auto-generated catch block

                e.printStackTrace();

            }
        }

    });

} catch (IOException e) {
    // TODO Auto-generated catch block

    e.printStackTrace();
}

}

public void readContent(Path filePath) throws IOException{

System.out.println("Reading file " + filePath);
Charset charset = Charset.forName("ISO-8859-1");
List<String> fileList = Files.readAllLines(filePath, charset);
System.out.println("" + fileList);
System.out.println("Finished reading!");
}

}
george b
  • 9
  • 2
  • are there cases where the keyword ENTER is on one line and the next line contains your keyword XX? – RAZ_Muh_Taz Mar 02 '18 at 18:43
  • No. Everytime the word ENTER appears, i'm 100% positive that the next one is the XX i am looking for. There can be 50 spaces between ENTER and my XX word but it's still that one. The XX word won't be present on another line – george b Mar 02 '18 at 18:58
  • so you might want to read line by line yourself. Currently you call readAllLines which works fine for small files but if the file size becomes to big you will run into OutOfMemoryError excpetion. All that's left is to check if the line.contains("ENTER"), if it does then you can grab the word and add it to a container. I suggest using a hashmap where the key is the current file path and the value is an arraylist of strings, where the strings are those words found after the ENTER keyword – RAZ_Muh_Taz Mar 02 '18 at 19:12

0 Answers0