I have a way like this:

/home/user/download/ 

And a file, for example, file.txt . Surely there should be platform-independent standard methods for combining the path and file name. Simple line addition is not effective, because on the way to the file there may not be the last slash and in different OS slashes in different directions.

    3 answers 3

    Just use the File constructor.

     File result = new File(new File("/home/user/download/"), "file.txt"); 

      Why so? Effectively. Just need to replace the slash / by

       File.separator 
      • And if the path without a slash? - faoxis

      no matter what direction slashes for File

        File f = new File("C:\\temp/test.log"); if (f.exists()) { System.out.println(f.getAbsolutePath()); } 

      result

        C:\temp\test.log 

      As a colleague said, always try to use File.separator