How to Acess All Files From Folder URL in Java


===========ReadAllFile.java===============

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author anand
 */
public class ReadAllFile {
   
    public static void main (String [] args) throws IOException{
       
        File folder = new File("/home/anand/Anand1");
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);
   
      System.out.println("Read==>"+ content );
    /* do somthing with content */
  }
}
    }
}



==============================================
Make Sure That you create Anand1.txt and Anand2.txt inside Anand1 Folder then it will read the file from java code



Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Java Design pattern

Map class field to map