-2

I want to know how to get into the file just by using relative path instead of absolute path. the project is used by different users so we have not the same absolute path ?

sabri
  • 5
  • 5
  • Possible duplicate of [How to read text file from relative path in a project?](http://stackoverflow.com/questions/3844307/how-to-read-text-file-from-relative-path-in-a-project) – 8bittree May 18 '16 at 14:00

1 Answers1

-1

just use File.getName()

File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");
System.out.println(f.getName());

using String methods:

File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");  
System.out.println(f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf("\\")+1))
  • I think I didn't explain well, so what I want is to get into the file just with relative path instead of absolute path, because the project will be use by other users so we have not the same absolute path ? – sabri May 18 '16 at 13:53
  • I tried but the problem is that the method I use is static but the method Getclass() is not which is mean that I should create an instance of object. I create if but the url I get is null ?! – sabri May 18 '16 at 14:12
  • URL url = getClass().getResource("MT Stressee.xlsx"); return null , I tried to clean project but I still have null as result , The file "MT Stressee.xlsx" is in the same package with the class concerned . Please if anyone can help ! – sabri May 18 '16 at 14:57