Get file full path in java

后端 未结 2 1580
滥情空心
滥情空心 2021-02-09 02:46

When I pass File file to a method I\'m trying to get its full path like file.getAbsolutePath(); I always get the same result no matter which one I use

2条回答
  •  面向向阳花
    2021-02-09 03:14

    I think there is a way it may help you if and only if the file is in the program directory.

    first you get the program directory by :

    new File(".").getCanonicalPath()
    

    then :

    if file is inside a specific directory like folder\\filename the full path will be

    (new File(".").getCanonicalPath() + "\\folder\\filename")
    

    or if file is directly inside the program directory: the full path will be

    (new File(".").getCanonicalPath() + "\\filename")
    

    i wish this answer help you :)

提交回复
热议问题