tags:

views:

1241

answers:

2

My problem is ::

From a string like "/usr/folder1/folder2/filename.ext"

  • I have to extract out file name only for display (filename.ext only).
    • My question would be how should I do it? Splitting on "/" and taking the last element is one way but doesn't smell nice to me.
  • I have to create a hyperlink which uses URI of the file as the destination. That will be something similar to file://domain.com/usr/folder1/folder2/filename.ext

I looked at URI and URL interfaces in java.net but could not find anything useful there.

Also, in some cases, my file path can have COMMA, SPACE etc (Windows folders). So, keep that in mind when giving any ideas.

+3  A: 

You should have a look to the File class. Especially to the getName() method.

Nicolas
+4  A: 

You could try something like this:

File file = new File("/usr/folder1/folder2/filename.ext");
System.out.println(file.getName());

I wasn't sure whether this would work if the file does not exist, but have just tried it and it appears to work OK.

trilobite
thx, I was to lazy to give the code sample. ;)
Nicolas