Solved

java string.split file directory path

Posted on 2011-03-01
5
2,440 Views
Last Modified: 2012-05-11
Hello

Trying to return the file name from a the directory path:

Its not splitting the string at all.

public String getFilenameFromFile(String file)
    {
        String delims = "[\\]"; < -- when I debug this is the symbol seperating the folders
        String[] tokens = file.split(delims);
        int num = tokens.length;
        String lfilename = tokens[num].toString();
        return filename;
    }

Open in new window

0
Comment
Question by:AndyC1000
  • 3
5 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 35013841
can you print the 'file' variable contents?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35013852


http://stackoverflow.com/questions/1099859/how-to-split-a-path-platform-independent

 offer
this solution:

String[] subDirs = path.split(Pattern.quote(File.separator));
0
 
LVL 47

Accepted Solution

by:
for_yan earned 83 total points
ID: 35013876
And I tested it

String[] subDirs = path.split(Pattern.quote(File.separator));

and it really works

"C:\\temt\\text.txt"  -->

C:
temt
text.t



0
 
LVL 92

Assisted Solution

by:objects
objects earned 42 total points
ID: 35013878
try this:

File f = new File(file);
String filename = f.getName();
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 83 total points
ID: 35013879
And I tested it

String[] subDirs = path.split(Pattern.quote(File.separator));

and it really works

"C:\\temt\\text.txt"  -->

C:
temt
text.txt
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now