user8241522
user8241522

Reputation: 37

I need help reading data from all files in a directory

I have a piece of code that iterates over all the files in a directory. But I am stuck now at reading the content of the file into a String object.

public String filemethod(){
        if (path.isDirectory()) {
            files = path.list();
            String[] ss;
            for (int i = 0; i < files.length; i++) {
                ss = files[i].split("\\.");
                if (files[i].endsWith("txt"))
                    System.out.println(files[i]);
            }
        }

    return String.valueOf(files);
}

Upvotes: 2

Views: 159

Answers (3)

Supun Amarasinghe
Supun Amarasinghe

Reputation: 1443

Faced with a similar problem and wrote a code a while back. This will read the content of all files of a directory.

May require adjustments based on your file directories but its tried and tested code.Hope this helps :)

package FileHandling;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class BufferedInputStreamExample {

    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    public void readFile(File folder) {
        ArrayList<File> myFiles = listFilesForFolder(folder);

        for (File f : myFiles) {
            String path = f.getAbsolutePath();

            //Path of the file(Optional-You can know which file's content is being printed)
            System.out.println(path);
            File infile = new File(path);

            try {
                fis = new FileInputStream(infile);
                bis = new BufferedInputStream(fis);
                dis = new DataInputStream(bis);

                while (dis.available() != 0) {
                    String line = dis.readLine();
                    System.out.println(line);
                }

            } catch (IOException e) {
            } finally {
                try {
                    fis.close();
                    bis.close();
                    dis.close();
                } catch (Exception ex) {
                }
            }
        }
    }

    public ArrayList<File> listFilesForFolder(final File folder){
        ArrayList<File> myFiles = new ArrayList<File>();

        for (File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                myFiles.addAll(listFilesForFolder(fileEntry));
            } else {
                myFiles.add(fileEntry);
            }
        }
        return myFiles;
    }
}

Main method

package FileHandling;

import java.io.File;

public class Main {

public static void main(String args[]) {

    //Your directory here
    final File folder = new File("C:\\Users\\IB\\Documents\\NetBeansProjects\\JavaIO\\files");

    BufferedInputStreamExample bse = new BufferedInputStreamExample();
    bse.readFile(folder);

    }
}

Upvotes: 2

Eritrean
Eritrean

Reputation: 16498

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;


public class ReadDataFromFiles {
    static final File DIRECTORY = new File("C:\\myDirectory");
    public static void main(String[] args) throws IOException {
        StringBuilder sb = new StringBuilder();
        //append content of each file to sb
        for(File f : getTextFiles(DIRECTORY)){
            sb.append(readFile(f)).append("\n");
        }
        System.out.println(sb.toString());
    }
    // get all txt files from the directory
    static File[] getTextFiles(File dir){
        FilenameFilter textFilter = (File f, String name) -> name.toLowerCase().endsWith(".txt");
        return dir.listFiles(textFilter);
    }
    // read the content of a file to string
    static String readFile(File file) throws IOException{
        return new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())), StandardCharsets.UTF_8);
    }
}

Upvotes: 1

Joris Schellekens
Joris Schellekens

Reputation: 9012

I would use following code:

public static Collection<File> allFilesInDirectory(File root) {
    Set<File> retval = new HashSet<>();
    Stack<File> todo = new Stack<>();
    todo.push(root);
    while (!todo.isEmpty()) {
        File tmp = todo.pop();
        if (tmp.isDirectory()) {
            for (File child : tmp.listFiles())
                todo.push(child);
        } else {
            if (isRelevantFile(tmp))
                retval.add(tmp);
        }
    }
    return retval;
}

All you need then is a method that defines what files are relevant for your usecase (for instance txt)

public static boolean isRelevantFile(File tmp) {
    // get the extension
    String ext = tmp.getName().contains(".") ? tmp.getName().substring(tmp.getName().lastIndexOf('.') + 1) : "";
    return ext.equalsIgnoreCase("txt");
}

Once you have all the files, you can easily get all the text with a little hack in Scanner

public static String allText(File f){
    // \\z is a virtual delimiter that marks end of file/string
    return new Scanner(f).useDelimiter("\\z").next();
}

So now, using these methods you can easily extract all the text from an entire directory.

public static void main(String[] args){
    File rootDir = new File(System.getProperty("user.home"));
    String tmp = "";
    for(File f : allFilesInDirectory(rootDir)){
         tmp += allText(f);
    }
    System.out.println(tmp);
}

Upvotes: 1

Related Questions