Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: How to get the file name from a String containing the path plus the file name?

Reply
Thread Tools

Re: How to get the file name from a String containing the path plus the file name?

 
 
Mike Schilling
Guest
Posts: n/a
 
      03-04-2010
www wrote:
> www wrote:
>> Hi,
>>
>> I have a String "dirA/dirB/dirC/file.txt". How to use String
>> manipulation technique to get "file.txt"?
>>
>> String fullName = "dirA/dirB/dirC/file.txt";
>>
>> int index = fullName.lastIndexOf("\\"); //here is a problem, if I
>> try lastIndexOf("\"), there is a compile error
>>
>> String fileName = fullName.subString(index); //but does not work
>>
>> Thank you very much.
>>
>> ps: I am working on linux.

>
> Sorry. I got it work. It was a typo:
>
> int index = fullName.lastIndexOf("\\"); //typo
>
> int index = fullName.lastIndexOf("/"); //now it is correct


Better, becasue it will handle more general file name syntax (e.g. both /
and \ on Wondows):

File f = new File(str);
String name = f.getName();


 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      03-04-2010
On 3/4/2010 12:31 PM, Mike Schilling wrote:
> www wrote:
>> www wrote:
>>> Hi,
>>>
>>> I have a String "dirA/dirB/dirC/file.txt". How to use String
>>> manipulation technique to get "file.txt"?
>>>
>>> String fullName = "dirA/dirB/dirC/file.txt";
>>>
>>> int index = fullName.lastIndexOf("\\"); //here is a problem, if I
>>> try lastIndexOf("\"), there is a compile error
>>>
>>> String fileName = fullName.subString(index); //but does not work
>>>
>>> Thank you very much.
>>>
>>> ps: I am working on linux.

>>
>> Sorry. I got it work. It was a typo:
>>
>> int index = fullName.lastIndexOf("\\"); //typo
>>
>> int index = fullName.lastIndexOf("/"); //now it is correct

>
> Better, becasue it will handle more general file name syntax (e.g. both /
> and \ on Wondows):
>
> File f = new File(str);
> String name = f.getName();

I wonder if it works on VMS, where the native structure is strange


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      03-05-2010
On 04-03-2010 17:22, Daniel Pitts wrote:
> On 3/4/2010 12:31 PM, Mike Schilling wrote:
>> www wrote:
>>> www wrote:
>>>> I have a String "dirA/dirB/dirC/file.txt". How to use String
>>>> manipulation technique to get "file.txt"?
>>>>
>>>> String fullName = "dirA/dirB/dirC/file.txt";
>>>>
>>>> int index = fullName.lastIndexOf("\\"); //here is a problem, if I
>>>> try lastIndexOf("\"), there is a compile error
>>>>
>>>> String fileName = fullName.subString(index); //but does not work
>>>>
>>>> Thank you very much.
>>>>
>>>> ps: I am working on linux.
>>>
>>> Sorry. I got it work. It was a typo:
>>>
>>> int index = fullName.lastIndexOf("\\"); //typo
>>>
>>> int index = fullName.lastIndexOf("/"); //now it is correct

>>
>> Better, becasue it will handle more general file name syntax (e.g. both /
>> and \ on Wondows):
>>
>> File f = new File(str);
>> String name = f.getName();

> I wonder if it works on VMS, where the native structure is strange


Java on VMS prefer Unix style paths.

So "dirA/dirB/dirC/file.txt" and not "[.dirA.dirB.dirC]file.txt" ...

Arne


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Invoking c function in a c plus plus function... Rahul C++ 9 03-25-2008 05:24 PM
INPUT Type="FILE" - Just want to get the file name and path as string kevin@webpageconcepts.com ASP .Net 4 08-02-2006 05:47 PM
C plus plus vs C Sharp The LoxFather C Programming 23 08-14-2003 03:51 AM
C plus plus vs C Sharp The LoxFather C++ 23 08-14-2003 03:51 AM



Advertisments