Creating a program to be run on more than one platform such as Windows and Linux our program need to understand the difference between both platform. The simplest thing for instance is the file separator. Windows use "\"
(backslash) while Linux use "/"
(forward slash).
To avoid manual checking for the operating system we can get the file separator symbol from the system property using the file.separator
key.
package org.kodejava.lang;
public class FileSeparatorExample {
public static void main(String[] args) {
// file.separator system property return the correct file
// separator for each different platform (Windows = \),
// (Linux = /)
String dataFolder = System.getProperty("user.dir") +
System.getProperty("file.separator") + "data";
System.out.println("Data Folder = " + dataFolder);
}
}
Latest posts by Wayan (see all)
- How do I compile and execute a JDK preview features with Maven? - December 8, 2023
- How do I sum object property using Stream API? - December 7, 2023
- How do I iterate through date range in Java? - October 5, 2023