Java cross platform definition for file path location

I am currently compiling the following code that does not compile. The else if reports that ' ;' expected ;' expected . I don’t understand why I can’t use else if for this scenario?

 public class FileConfiguration { private String checkOs() { String path = ""; if (System.getProperty("os.name").startsWith("Windows")) { // includes: Windows 2000, Windows 95, Windows 98, Windows NT, Windows Vista, Windows XP path = "C://Users//..."; } elseif (System.getProperty("os.name").startsWith("Mac")) { path = "///Users//..."; } return path; } // declare paths for file source and destination String destinationPath = path; String sourcePath = path; 
+4
source share
3 answers

It would be better if you used user.name and user.home . You can also get the delimiter using file.separator . Check it out . These properties will really help you do this more cleanly without checking the OS.

Then you also need to switch to using else if , not elseif ...

+6
source

elseif does not exist in java. You should use else if :

 if (a) { // code } else if (b) { // code } 
+2
source

There is no elseif keyword in java. You should say else if (note the space)

+1
source

Source: https://habr.com/ru/post/1396825/


All Articles