Anil Kumar
Anil Kumar

Reputation: 133

how to get name of file using Index of command in java

public class myclass {

    public static void main(String args[]) {
        String filepath = "E:\\SW\\eclipse-jee-helios-SR1-win32\\eclipse\\CBDTFiles\\Circulars\\CBDTLaws\\HTMLFiles\\file1.htm";
        String w=filepath.replace("E:\\SW\\eclipse-jee-helios-SR1-win32\\eclipse\\CBDTFiles\\Circulars\\CBDTLaws\\HTMLFiles\\", "");
        System.out.println(w);

    }

}

This is My code i am able to display file name using replace of command But i want to get file Using Index of command how i will get Index of HTMLFiles from that we can get file name because file is comingDynamically

Upvotes: 0

Views: 1774

Answers (2)

user207421
user207421

Reputation: 310913

Why are you insisting on indexOf()? The correct way is new File(filepath).getName().

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 213243

Use String#lastIndexOf(String) with String#substring(int):

String fileName = filepath.substring(filepath.lastIndexOf("\\") + 1);

Upvotes: 3

Related Questions