?
Solved

Read filenames from a directory

Posted on 2003-03-14
5
Medium Priority
?
186 Views
Last Modified: 2012-08-13
How would I read the filenames in a directory...for that matter, how would I read the directory names as well?  I'm a newbie to java.  I don't need to use the file dialog controls, I need this to be dynamic (i think that's the word i'm looking for).

Let me know if I need to be clearer.

TIA
DDS
0
Comment
Question by:dds110
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:sct75
ID: 8136272
You could ask user to enter in the directory name from the command line or any GUI widget, such as TextField, etc., from a graphical interface.

By obtaining such input, you could do following
java.io.File file = new java.io.File(userInput);

The file.isDirectory() will tell if the user input represents a directory.

The the file.list() will return you a String[] that contains the files and directories in the directory, including the "." (self) and ".." (the parent). Or the file.listFiles() returns you an array of abstract pathnames denoting the files in the directory.
0
 
LVL 9

Accepted Solution

by:
yongsing earned 200 total points
ID: 8136288
The following reads the C:\temp directory, and display the files or directories names.

File file = new File("C://temp");
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
    System.out.print(files[i].getName() + " - ");
    if (files[i].isDirectory()) {
        System.out.println("Directory");
    } else {
        System.out.println("File");
    }
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 8136317
 The following reads all the files into a directory and directories within directories:

import java.util.*;

public class GetAllFilesInADirectory
{
     private static Vector v = new Vector();
     
     public static void main(String [] arguments)
     {
          Vector vec = getAllFiles(new java.io.File("c:/ColumbusXMLFiles"));
     
          for (int i=0; i<vec.size(); i++)
          {
               System.out.println(vec.elementAt(i));
          }
     }
     
     public static Vector getAllFiles(java.io.File location)
     {
          java.io.File [] list = location.listFiles();
         
          for (int i=0; i < list.length; i++)
          {
               v.addElement(list[i]);

               if ((list[i]).isDirectory())
               {
                    getAllFiles(list[i]);
               }
          }
         
          return v;
     }
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 8136322
 Just change this bit: "c:/ColumbusXMLFiles" with your own directory name.
0
 
LVL 9

Expert Comment

by:yongsing
ID: 8136339
Mistake in my code. First line should be:

File file = new File("C:/temp");

or

File file = new File("C:\\temp");
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question