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
Solved

How to extract a file name from file path in JAVA

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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?
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 …

789 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