Please tell me how to count from the directory (folder) all files (they are in txt format) and split their contents into words. I tried to write:

import java.io.*; import java.util.*; public class Fil { public static void main(String[] args) throws IOException { File files = new File("C://Instructions"); List < String > textFiles = new ArrayList < String > (); for (File file: files.listFiles()) { try { BufferedReader br = new BufferedReader(new FileReader(file)); String str = br.readLine(); StringTokenizer st = new StringTokenizer(str); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } } catch (IOException e) { e.printStackTrace(); } } } } 
  • And how did your attempt end? What difficulties or error messages? Format the code. - 0xdb

1 answer 1

To read all the txt files from a folder, try this:

 File myfolder = new File("/путь к папке"); File[] listOfFiles = myfolder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { File file = listOfFiles[i]; if (file.isFile() && file.getName().endsWith(".txt")) { String content = FileUtils.readFileToString(file); /* обработка */ } } 

And to break into words is quite simple: Enumerate the entire text of the file by character. If the next character is a space or a punctuation mark, then we skip them and write the previous block of characters as a new element of the array. Or use:

 String[] strArray = str.split(" "); 

And then "clean" the elements of the array from punctuation.

  • Thanks, please tell me how to deal with FileUtils, it gives an error. Inscribed import org.apache.commons.io.FileUtils. (writes org. does not exist). In org.apache there is lang, logging, but io.FileUtils is not. - Lana
  • FileUtils is a class from the apache org.apache.commons package, so you need to download org.apache.commons.io.jar and then embed this file. I have Eclipse, where it is done like this: open project properties -> Java Build Path -> Libraries tab -> Add External Jars. So you can add a jar. You can download from here . - TerryFrog
  • Thank you so much, everything turned out) - Lana