[Solved] Java, automatic reading file from a directory [closed]


Reads & prints the content

public static void main(String[] args) {
    List<String> li=new TestClass().textFiles("your Directory");
    for(String s:li){
        try(BufferedReader br = new BufferedReader(new FileReader(s))) {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            String everything = sb.toString();
            System.out.println(everything);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

For getting all Text files in the Directory

List<String> textFiles(String directory) {
      List<String> textFiles = new ArrayList<String>();
      File dir = new File(directory);
      for (File file : dir.listFiles()) {
        if (file.getName().endsWith((".txt"))) {
          textFiles.add(file.getPath());
        }
      }
      return textFiles;
    }

solved Java, automatic reading file from a directory [closed]