Difference between File.separator and slash in paths

后端 未结 14 1415
执笔经年
执笔经年 2020-11-22 13:15

What is the difference between using File.separator and a normal / in a Java Path-String?

In contrast to double backslash \\\\

14条回答
  •  自闭症患者
    2020-11-22 13:40

    If you are trying to create a File from some ready path (saved in database, per example) using Linux separator, what should I do?

    Maybe just use the path do create the file:

    new File("/shared/folder/file.jpg");
    

    But Windows use a different separator (\). So, is the alternative convert the slash separator to platform independent? Like:

    new File(convertPathToPlatformIndependent("/shared/folder"));
    

    This method convertPathToPlatformIndependent probably will have some kind of split by "/" and join with File.separator.

    Well, for me, that's not nice for a language that is platform independent (right?) and Java already support the use of / on Windows or Linux. But if you are working with paths and need to remember to this conversion every single time this will be a nightmare and you won't have any real gain for the application on the future (maybe in the universe that @Pointy described).

提交回复
热议问题