2

In the program below I'm currently reading files in a directory. However I'd like to only read .txt files. How can I read only the .text files located in the directory.

import java.io.*;

public class Data {

    public static void main(String[] args) throws IOException {
        String target_dir = "C:\\Files";
        String textfile;
        File dir = new File(target_dir);
        File[] files = dir.listFiles();

        for (File textfiles : files) {
            if (textfiles.isFile()) {

                BufferedReader inputStream = null;

                try {
                    inputStream = new BufferedReader(new FileReader(textfiles));
                    String line;

                    while ((line = inputStream.readLine()) != null) {
                        System.out.println(line);
                    }
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                }
            }
        }
    }

}
Razib
  • 10,965
  • 11
  • 53
  • 80
FullNelson
  • 99
  • 2
  • 13

7 Answers7

3

You can use java.io.FilenameFilter to filter files in a directory.

import java.io.*;

public class Data {

    public static void main(String[] args) throws IOException {
        String target_dir = "C:\\Files";
        File dir = new File(target_dir);
        FilenameFilter textFileFilter = new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".txt");
            }
        };
        File[] files = dir.listFiles(textFileFilter);

       for (File textfiles : files) {
           if (textfiles.isFile()) {

               BufferedReader inputStream = null;

               try {
                   inputStream = new BufferedReader(new FileReader(textfiles));
                   String line;

                   while ((line = inputStream.readLine()) != null) {
                       System.out.println(line);
                   }
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                }
            }
        }
    }
}

}
barunsthakur
  • 1,196
  • 1
  • 7
  • 18
2

Try checking the extension

import java.io.*;

public class Data {

    public static void main(String[] args) throws IOException {
        String target_dir = "C:\\Files";
        String textfile;
        File dir = new File(target_dir);
        File[] files = dir.listFiles();

        for (File textfiles : files) {
            if (textfiles.isFile() && textfiles.getName().endsWith(".txt")) {

                BufferedReader inputStream = null;

                try {
                    inputStream = new BufferedReader(new FileReader(textfiles));
                    String line;

                    while ((line = inputStream.readLine()) != null) {
                        System.out.println(line);
                    }
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                }
            }
        }
    }
}
Ryan
  • 2,058
  • 1
  • 15
  • 29
1

Use Files.probeContentPath() to get a mime of the file.

Distjubo
  • 959
  • 6
  • 21
0

You can use a FileNameFilter to do that work for you. Your code could look like this:

    File[] files = dir.listFiles(new FilenameFilter() {         
        @Override
        public boolean accept(File dir, String name) {
            if (name.toLowerCase().endsWith(".txt")) return true;
            return false;
        }
    });

Then you know that files contains only files that end with .txt, or whatever other conditions you need.

Always Learning
  • 5,510
  • 2
  • 17
  • 34
0

I like the DirectoryScanner method which uses ant.jar.

DirectoryScanner scanner = new DirectoryScanner();
scanner.setIncludes(new String[]{"**/*.txt"});
scanner.setBasedir("C:\\Files");
scanner.setCaseSensitive(false);
scanner.scan();
String[] files = scanner.getIncludedFiles();

This is from Misha Here. But as you can see there are many ways to do this.

Community
  • 1
  • 1
ghg565
  • 422
  • 6
  • 15
0

Write a method that returns all the .txt files in a given directory.

import java.io.File;
import java.io.FilenameFilter;

public class Data {
         public File[] finder( String dirName){
                File dir = new File(dirName);

                return dir.listFiles(new FilenameFilter() { 
                         public boolean accept(File dir, String filename)
                              { return filename.endsWith(".txt"); }
                } );

            }
        public static void main(String[] args) throws IOException {
            String target_dir = "C:\\Files";
            String textfile;
            File dir = new File(target_dir);
            File[] files = finder(target_dir);

            for (File textfiles : files) {
                if (textfiles.isFile() && textfiles.getName().endsWith("txt")) {

                    BufferedReader inputStream = null;

                    try {
                        inputStream = new BufferedReader(new FileReader(textfiles));
                        String line;

                        while ((line = inputStream.readLine()) != null) {
                            System.out.println(line);
                        }
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    }
                }
            }
        }
    }
SoftwareCarpenter
  • 3,835
  • 3
  • 25
  • 37
0
DIR* dir = opendir("."); // to open current dir

entity = readdir(dir);

while(entity != NULL){

    sprintf(filename,"%s", entity->d_name);
    
    len = strlen(filename);

    if(strcmp(filename+len-4,".txt")) {entity = readdir(dir); continue;} 

   // operation to execute.

    entity = readdir(dir);
    
}

//like this also we can do this task

shridhar
  • 1
  • 2