Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File.seperator help

Status
Not open for further replies.

okiiyama

IS-IT--Management
Jan 3, 2003
269
US
I would like to list files and folders in a directory

for example I would like to list the files and folders in C:\testFolder\anotherFolder

How do I use the File class in order to do this... Also, it needs to be a program that can possibly be ran on other systems. So I guess this is why I need to use the File.seperator deal. I know that I will need to use list(), and the list gets returned as an array, but other than that I don't know what to do.

Any advice would be magnificent.
Thanks
 
Transform each item in the list to a string and you will see the list of filenames in the listed directory.

Ion Filipski
1c.bmp
 
You do not have to worry about file separators - this is all taken care for you.
 
And in case you need it, for example to "manually" build a path, you can retrieve it with

System.getProperty("file.separator");

Cheers.

Dian.
 
You don't need the file separator for building a path.

Whether on unix, linux or Win32, if you use "/", it will be interpreted properly for each OS.
 
Nope.

The "/" will not be correctly interpreted by Windows, for example if you're trying to use the command line.

Cheers.

Dian.
 
I can assure you that this code :

File f = new File("C:/a/b/c");

will be correctly interpreted by java on a Win32 system to be "C:\a\b\c" when interacting with the OS IO dlls.
 
Could you assure the same for

System.exec("dir /somedir/anotherdir");

?, for example.

Cheers.

Dian.
 
Of course not ! This is basically shelling out to the OS - so any of Java's cross-platform capabilities are lost.

The original post was asking about the usuage of file separators when using the File class - not about executing OS specific scripts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top