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!");
}
}