Solved

java string.split file directory path

Posted on 2011-03-01
5
2,488 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 98
What is the latest versions eclipse neon 2 139
Unable to start eclipse ? 17 128
java.io.BufferedReader   readLine()  //returns the entire file ? 4 59
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

914 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

15 Experts available now in Live!

Get 1:1 Help Now