Solved

How to extract a file name from file path in JAVA

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

Accepted Solution

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

Assisted Solution

by:kouroshparsa
kouroshparsa earned 25 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

Technology Partners: 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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
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 …
Simple Linear Regression

626 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