Solved

java string.split file directory path

Posted on 2011-03-01
5
2,676 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

724 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