Solved

Using Java to get file names and directory

Posted on 2011-02-19
8
588 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 102
Bot application - advice 3 39
Java basic valueOf question 1 29
Need Help! Getting a syntax error and don't understand why 3 31
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…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

828 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