Solved

Using Java to get file names and directory

Posted on 2011-02-19
8
585 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 49
array6 challenfge 6 65
Python Assistance 7 36
Problem to start Neon 20 54
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now