Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback, please email oracle-forums_moderators_us@oracle.com

How can I read only text files in a directory.

2801625Apr 30 2015 — edited May 1 2015

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

  }

  }

  }

  }

  }

}

This post has been answered by aJohny on Apr 30 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 29 2015
Added on Apr 30 2015
5 comments
1,001 views