This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

How can I read only text files in a directory.

2801625
2801625 Member Posts: 57
edited May 1, 2015 10:22AM in New To Java

I've written a program to read files in a directory but I'd like for it to only read the text files of that 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);

  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();

  }

  }

  }

  }

  }

}

Tagged:

Best Answer

  • aJohny
    aJohny PerthMember Posts: 4,856 Silver Crown
    edited Apr 30, 2015 11:58AM Answer ✓

    You have mentioned you want to read only text files.

    If you are referring to some specific set of extentions, you can filter based on that.

    ex: you want to read only .txt files, you can add an if condition as below :

              if(textfiles.getName().endsWith(".txt")) {
                  // Add your code here
              }
    

    Cheers

    AJ

Answers

  • aJohny
    aJohny PerthMember Posts: 4,856 Silver Crown
    edited Apr 30, 2015 11:15AM

    Inside you loop you could get the file name using

    textfiles.getName();

    You could filter with the file extension.

    Cheers

    AJ

  • 2801625
    2801625 Member Posts: 57
    edited Apr 30, 2015 11:27AM

    Can you explain what you mean by filter with the file extension.

  • aJohny
    aJohny PerthMember Posts: 4,856 Silver Crown
    edited Apr 30, 2015 11:58AM Answer ✓

    You have mentioned you want to read only text files.

    If you are referring to some specific set of extentions, you can filter based on that.

    ex: you want to read only .txt files, you can add an if condition as below :

              if(textfiles.getName().endsWith(".txt")) {
                  // Add your code here
              }
    

    Cheers

    AJ

  • 2801625
    2801625 Member Posts: 57
    edited Apr 30, 2015 11:59AM

    I was able to read only the text files in the directory checking the extension using textfiles.getname and textfiles.endswith.

    Thanks for the help AJ.

         for (File textfiles : files) {

         if (textfiles.isFile() && textfiles.getName().endsWith("txt")) {

    2801625
  • aJohny
    aJohny PerthMember Posts: 4,856 Silver Crown
    edited May 1, 2015 10:22AM

    If all good, Please close the thread by marking the helpful and correct answers.

This discussion has been closed.