Solved

Using Java to get file names and directory

Posted on 2011-02-19
8
589 Views
Last Modified: 2012-05-11
Hi,  experts

I would like use JAVA to

1. get the current directory. for example my current directory in my java code is in (c:\abc)


2. After that I want to get ONLY files without director in to File[] something like below
File folder = new File("c:/abc");
File[] listOfFiles = folder.listFiles();

could you please teach me how can I can the
   (1) file name only. for example test.txt
   (2) file name with full path. for example c:\abc\test.txt
0
Comment
Question by:rmtogether
[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
  • 5
  • 2
8 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 375 total points
ID: 34933603


After you got the list of files you can check for each file
if it is directory or file
just using File boolean methods

File f;
f.isDirectory()

If it is not directory - then it is a file

f.getName() - will give you simple file name

f.getAbsolutePath(); will give you absolute path

0
 
LVL 47

Expert Comment

by:for_yan
ID: 34933627

This is how you get current working directory:
http://www.roseindia.net/java/example/java/io/GetCurrentDir.shtml
0
 

Author Comment

by:rmtogether
ID: 34933629
it seems to me the files also include hidden file of the directory. Can I exclude those hidden files?
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 375 total points
ID: 34933663

Something like that:

File folder = new File("c:/abc");
File[] listOfFiles = folder.listFiles();

System.out.println("Current folder : " + System.getProperty("user.dir");

for(int j=0; j<listOfFiles.length; j++){
if(listOfFiles[j].isDirectory()){
System.out.println(listOfFiles[j].getName() + " is a directory");

}
else
{
System.out.println(listOfFiles[j].getName() + " is a file");

}
Sytem.out.println("Its full path is : " + listFiles[j].getAbsolutePath());
Sytem.out.println("");

}
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34933731

methid File.isHidden() should return boolena which you can check
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 375 total points
ID: 34933737
I think like that will omit hidden files:

File folder = new File("c:/abc");
File[] listOfFiles = folder.listFiles();

System.out.println("Current folder : " + System.getProperty("user.dir");

for(int j=0; j<listOfFiles.length; j++){

if(listOfFiles[j].isHidden())continue;
if(listOfFiles[j].isDirectory()){
System.out.println(listOfFiles[j].getName() + " is a directory");

}
else
{
System.out.println(listOfFiles[j].getName() + " is a file");

}
Sytem.out.println("Its full path is : " + listFiles[j].getAbsolutePath());
Sytem.out.println("");

}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
ID: 34933769
Use a FilenameFilter
File[] n = new File(System.getProperty("user.dir")).listFiles(new FilenameFilter() {
	    public boolean accept(File f, String name) {
		return !f.isHidden();
	    }
	});
	System.out.println(java.util.Arrays.toString(n));

Open in new window

0
 

Author Closing Comment

by:rmtogether
ID: 34934061
thank you so much
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 video teaches viewers about errors in exception handling.

751 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