Klasse SystemUtil

java.lang.Object
com.github.hypfvieh.util.SystemUtil

public final class SystemUtil extends Object
Utility-Class with various operating system related helper methods.
Seit:
v0.0.5 - 2015-08-05
Autor:
hypfvieh
  • Felddetails

    • FILE_SEPARATOR

      public static final String FILE_SEPARATOR
      Character that separates components of a file path. This is "/" on UNIX and "\" on Windows.
    • LINE_SEPARATOR

      public static final String LINE_SEPARATOR
      Sequence used by operating system to separate lines in text files.
    • TMP_DIR

      public static final String TMP_DIR
      The system's temporary-file directory.
  • Methodendetails

    • getOs

      public static String getOs()
      Determines the operating system's type and version.
      Gibt zurück:
      the OS type and version as a string
    • getHostName

      public static String getHostName()
      Gets the host name of the local machine.
      Gibt zurück:
      host name
    • getWorkingDirectory

      public static String getWorkingDirectory()
      Returns the current working directory.
      Gibt zurück:
      current working dir
    • getRunningClassPath

      public static String getRunningClassPath()
      Returns the running class path.
      Gibt zurück:
      String with classpath
    • getTempDir

      public static String getTempDir()
      Returns the temp directory of this platform.
      Gibt zurück:
      temp directory
    • getJavaVersion

      public static String getJavaVersion()
      Determines the Java version of the executing JVM.
      Gibt zurück:
      Java version
    • getCurrentUser

      public static String getCurrentUser()
      Determines the current logged on user.
      Gibt zurück:
      logged on user
    • concatFilePath

      public static String concatFilePath(boolean _includeTrailingDelimiter, String... _parts)
      Concats a path from all given parts, using the path delimiter for the currently used platform.
      Parameter:
      _includeTrailingDelimiter - include delimiter after last token
      _parts - parts to concat
      Gibt zurück:
      concatinated string
    • concatFilePath

      public static String concatFilePath(String... _parts)
      Concats a path from all given parts, using the path delimiter for the currently used platform. Does not include trailing delimiter.
      Parameter:
      _parts - parts to concat
      Gibt zurück:
      concatinated string
    • appendSuffixIfMissing

      public static String appendSuffixIfMissing(String _str, String _suffix)
      Append a suffix to the string (e.g. filename) if it doesn't have it already.
      Parameter:
      _str - string to check
      _suffix - suffix to append
      Gibt zurück:
      string with suffix or original if no suffix was appended
    • appendTrailingDelimiter

      public static String appendTrailingDelimiter(String _filePath)
      Appends the OS specific path delimiter to the end of the String, if it is missing.
      Parameter:
      _filePath - file path
      Gibt zurück:
      String
    • createTempDirectory

      public static File createTempDirectory(String _path, String _name, boolean _deleteOnExit)
      Creates a new temporary directory in the given path.
      Parameter:
      _path - path
      _name - directory name
      _deleteOnExit - delete directory on jvm shutdown
      Gibt zurück:
      created Directory, null if directory/file was already existing
    • createTempDirectory

      public static File createTempDirectory(String _path, String _prefix, int _length, boolean _timestamp, boolean _deleteOnExit)
      Creates a temporary directory in the given path. You can specify certain files to get a random unique name.
      Parameter:
      _path - where to place the temp folder
      _prefix - prefix of the folder
      _length - length of random chars
      _timestamp - add timestamp (yyyyMMdd_HHmmss-SSS) to directory name
      _deleteOnExit - mark directory for deletion on jvm termination
      Gibt zurück:
      file
    • isDebuggingEnabled

      public static boolean isDebuggingEnabled()
      Examines some system properties to determine whether the process is likely being debugged in an IDE or remotely.
      Gibt zurück:
      true if being debugged, false otherwise
    • getFileExtension

      public static String getFileExtension(String _fileName)
      Extracts the file extension (part behind last dot of a filename). Only returns the extension, without the leading dot.
      Parameter:
      _fileName - filename
      Gibt zurück:
      extension, empty string if no dot was found in filename or null if given String was null
    • getFileExtension

      public static String getFileExtension(File _file)
      Extracts the file extension (part behind last dot of a filename). Only returns the extension, without the leading dot.
      Parameter:
      _file - file
      Gibt zurück:
      extension, empty string if no dot was found in filename or null if given String was null
    • getFileIfReadable

      public static File getFileIfReadable(String _file) throws IOException
      Checks if given String is a valid file, e.g. file exists, is really a file and can be read. Throws IOException or Subclass of it, if file is either non-existing, not a file or unreadable.
      Parameter:
      _file - filename
      Gibt zurück:
      file object, never null
      Löst aus:
      IOException - if file could not be read
    • formatBytesHumanReadable

      public static String formatBytesHumanReadable(long _bytes, boolean _use1000BytesPerMb)
      Formats a file size given in byte to something human readable.
      Parameter:
      _bytes - size in bytes
      _use1000BytesPerMb - use 1000 bytes per MByte instead of 1024
      Gibt zurück:
      String
    • getApplicationVersionFromJar

      public static String getApplicationVersionFromJar(Class<?> _class, String _default)
      Read the JARs manifest and try to get the current program version from it.
      Parameter:
      _class - class to use as entry point
      _default - default string to use if version could not be found
      Gibt zurück:
      version or null
    • guessDefaultTerminal

      public static String guessDefaultTerminal()
      Tries to find the "default" terminal emulator. This will be cmd.exe on windows and may vary on linux/unix systems depending on installed terminal programs. On linux/unix there is no generic way to find the default terminal, so all known terminal programs will be tried until any of them is found.
      Gibt zurück:
      String with terminal name or null if terminal could not be determined
    • normalizePath

      public static String normalizePath(String _path, boolean _appendFinalSeparator)
      Normalize a file system path expression for the current OS. Replaces path separators by this OS's path separator. Appends a final path separator if parameter is set and if not yet present.
      Parameter:
      _path - path
      _appendFinalSeparator - controls appendix of separator at the end
      Gibt zurück:
      normalized path
    • normalizePath

      public static String normalizePath(String _path)
      Normalize a file system path expression for the current OS. Replaces path separators by this OS's path separator. Appends a final path separator unless present.
      Parameter:
      _path - path
      Gibt zurück:
      normalized path
    • isMacOs

      public static boolean isMacOs()
      Checks if the running OS is a MacOS/MacOS X.
      Gibt zurück:
      true if MacOS (or MacOS X), false otherwise
    • getMacOsMajorVersion

      public static String getMacOsMajorVersion()
      Tries to get the current version of MacOS/MacOS X. The version usually looks like '10.13.4', where the part behind the last dot represents the patchlevel. The major version in this case would be '10.13'.
      Gibt zurück:
      version without patchlevel or null
    • deleteRecursively

      public static void deleteRecursively(String _fileOrDirectoryPath, boolean _stopOnError) throws IOException
      Delete all files and directories in the given path recursively.
      Parameter:
      _fileOrDirectoryPath - path to delete
      _stopOnError - if true throw an exception and exit on any error
      Löst aus:
      IOException - if error occurs during deletion
    • deleteRecursivelyQuiet

      public static void deleteRecursivelyQuiet(String _fileOrDirectoryPath)
      Delete all files and directories in the given path recursively and without throwing any exception.
      Parameter:
      _fileOrDirectoryPath - file or directory to delete