Solved

How to extract a file name from file path in JAVA

Posted on 2004-08-10
4
2,464 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
4 Comments
 

Accepted Solution

by:
yehiaeg earned 25 total points
Comment Utility
just search for the last '\' or '/' and remove everything before
0
 
LVL 2

Assisted Solution

by:kouroshparsa
kouroshparsa earned 25 total points
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 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

10 Experts available now in Live!

Get 1:1 Help Now