Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using Java to get file names and directory

Posted on 2011-02-19
8
Medium Priority
?
591 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 1500 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1500 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 1500 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

722 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