• Post Reply Bookmark Topic Watch Topic
  • New Topic

get file name using java on unix platform  RSS feed

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you create the File object? Using / or \ as directory separator? Because on Unix you should use /; \ is valid inside file names.
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes david, i noticed that. Thanks
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!