Solved

Using Java to get file names and directory

Posted on 2011-02-19
8
587 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

786 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