File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes get file name using java on unix platform Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "get file name using java on unix platform" Watch "get file name using java on unix platform" New topic
Author

get file name using java on unix platform

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 744
Hi,

I tried to use



where file1 is java.io.File object

to return the name of the file. This works fine on windows platform, but on unix it returns the absolute path. Is there any other way to get the file name which works both on windows and unix ?


Jigar Naik


Hari haran Ravi
Greenhorn

Joined: Aug 11, 2010
Posts: 26
Hi Jigar, If it returns an absolute path, process it using String Manipulations and then get the file name out of it..

I will give you an idea. In an absolute path the filename will start after the last slash in the absolute path. So find the index of the last slash using the lastIndexOf function and then use substring function to return the file name.

Regards
Hari
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

How did you create the File object? Using / or \ as directory separator? Because on Unix you should use /; \ is valid inside file names.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 744
i got an idea...

actually the application is deployed on unix so File.separator is returning / but i am uploading file from client(browser running on windows) which is giving \ as separator.

Thanks a lot.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Note that different browsers return different things for uploaded files--some return a path, some return just the file name. Make sure you code accordingly.
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 744
yes david, i noticed that. Thanks
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: get file name using java on unix platform
 
Similar Threads
problem in renaming a file with timestamp
Writing batch file
File class and the length method
file renameTo() method
Theoretical doubt from K&B book : File.separator