Created
August 4, 2021 15:19
-
-
Save joakime/fd8454fd437405e92445adb1f81a438c to your computer and use it in GitHub Desktop.
FindShortTempDir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fs; | |
import java.nio.file.FileSystems; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.List; | |
public class FindShortTempDir | |
{ | |
public static void main(String[] args) | |
{ | |
Path tmpDir = findTempDir("jetty.unixsocket.path"); | |
System.out.printf("tmpDir[jetty.unixsocket.path] = %s%n", tmpDir); | |
} | |
private static Path findTempDir(String systemPropertyName) | |
{ | |
String systemPropPath = System.getProperty(systemPropertyName); | |
if (systemPropPath != null && !systemPropPath.isBlank()) | |
{ | |
Path tmp = Paths.get(systemPropPath); | |
if (Files.exists(tmp) && Files.isDirectory(tmp)) | |
return tmp; | |
} | |
for (Path rootDir : FileSystems.getDefault().getRootDirectories()) | |
{ | |
for (String dirName : List.of("tmp", "var/tmp", "Temp")) | |
{ | |
Path tmp = rootDir.resolve(dirName); | |
if (Files.exists(tmp) && Files.isDirectory(tmp)) | |
return tmp; | |
} | |
} | |
return Paths.get(System.getProperty("java.io.tmpdir")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment