Solved

How to extract a file name from file path in JAVA

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

Suggested Solutions

Title # Comments Views Activity
VB.net and sql server 4 54
AvlTree-Node Data type 4 27
Create .bat File 16 73
Problems moving Excel files from local drive to server 4 36
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?

730 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