Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# java string.split file directory path

Posted on 2011-03-01
Medium Priority
2,951 Views
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;
}

0
Question by:AndyC1000
• 3

LVL 40

Expert Comment

ID: 35013841
can you print the 'file' variable contents?
0

LVL 47

Expert Comment

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

for_yan earned 332 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

objects earned 168 total points
ID: 35013878
try this:

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

LVL 47

Assisted Solution

for_yan earned 332 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

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
###### Suggested Courses
Course of the Month15 days, 21 hours left to enroll

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

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