Solved

java string.split file directory path

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 43
eclipse console opening separately 2 29
Is there a simpler dropbox system? 10 34
Is Applet the way to go for my drag and drop system? 8 25
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

829 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