Search code examples
javafilefileutils

Accessing .txt files from inside a folder (java)


I have a similar problem to this question, and I tried the answer that the question accepted:

File folder = new File("/path/to/files");
File[] listOfFiles = folder.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);
    /* do somthing with content */
  } 
}

Only when I tried compiling, it couldn't find the variable FileUtils. Is there a java package that I have to import at the beginning of my program to use class File methods? I've never used File objects before, so I'm trying to learn to be able to work with and manipulate them. Thanks.


Solution

  • This is Apache Commons FileUtils . http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html

    You would need to import import org.apache.commons.io.FileUtils;