Reputation: 133
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
Reputation: 310913
Why are you insisting on indexOf()
? The correct way is new File(filepath).getName().
Upvotes: 1
Reputation: 213243
Use String#lastIndexOf(String)
with String#substring(int)
:
String fileName = filepath.substring(filepath.lastIndexOf("\\") + 1);
Upvotes: 3