Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to extract a file name from file path in JAVA

Posted on 2004-08-10
4
Medium Priority
?
2,532 Views
Last Modified: 2008-01-16
Hello Everybody,

I have a file path like this:
C:/Documents and ettings/ravuri.DS17/Desktop/PBSFiles/Ram1234.txt.

I need to extract the file name i.e., Ram1234.txt.
And the important thing is always the file path is changing(like
C:/Ram1234.doc) and the extension of the file may change like...Ram1234.doc.
So how to extract the file name in JAVA from the path(whatever may be the path).
Can anybody help me in this regard.

Thanks for your time and patience.

Thx,
Rams
0
Comment
Question by:ramsin
2 Comments
 

Accepted Solution

by:
yehiaeg earned 100 total points
ID: 11764000
just search for the last '\' or '/' and remove everything before
0
 
LVL 2

Assisted Solution

by:kouroshparsa
kouroshparsa earned 100 total points
ID: 11777507
Piece of cake:

public class Demo
{
   public static void main(String[]args)
   {
      String str = "asshhs/dfhdfhdf/djgj/Ram1234.txt";
      String fileName = "";
      fileName=str.substring(1 + str.lastIndexOf("/"));

      System.out.println(fileName);

   }
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Simple Linear Regression
Starting up a Project

877 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