Package oshi.util

Class FileUtil

java.lang.Object
oshi.util.FileUtil

@ThreadSafe
public final class FileUtil
extends java.lang.Object
File reading methods
  • Method Summary

    Modifier and Type Method Description
    static int getIntFromFile​(java.lang.String filename)
    Read a file and return the int value contained therein.
    static java.util.Map<java.lang.String,​java.lang.String> getKeyValueMapFromFile​(java.lang.String filename, java.lang.String separator)
    Read a file and return a map of string keys to string values contained therein.
    static long getLongFromFile​(java.lang.String filename)
    Read a file and return the long value contained therein.
    static java.lang.String getStringFromFile​(java.lang.String filename)
    Read a file and return the String value contained therein.
    static long getUnsignedLongFromFile​(java.lang.String filename)
    Read a file and return the unsigned long value contained therein as a long.
    static java.util.List<java.lang.String> readFile​(java.lang.String filename)
    Read an entire file at one time.
    static java.util.List<java.lang.String> readFile​(java.lang.String filename, boolean reportError)
    Read an entire file at one time.
    static java.util.Properties readPropertiesFromFilename​(java.lang.String propsFilename)
    Read a configuration file from the sequence of context classloader, system classloader and classloader of the current class, and return its properties
    static java.lang.String readSymlinkTarget​(java.io.File file)
    Reads the target of a symbolic link

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • readFile

      public static java.util.List<java.lang.String> readFile​(java.lang.String filename)
      Read an entire file at one time. Intended primarily for Linux /proc filesystem to avoid recalculating file contents on iterative reads.
      Parameters:
      filename - The file to read
      Returns:
      A list of Strings representing each line of the file, or an empty list if file could not be read or is empty
    • readFile

      public static java.util.List<java.lang.String> readFile​(java.lang.String filename, boolean reportError)
      Read an entire file at one time. Intended primarily for Linux /proc filesystem to avoid recalculating file contents on iterative reads.
      Parameters:
      filename - The file to read
      reportError - Whether to log errors reading the file
      Returns:
      A list of Strings representing each line of the file, or an empty list if file could not be read or is empty
    • getLongFromFile

      public static long getLongFromFile​(java.lang.String filename)
      Read a file and return the long value contained therein. Intended primarily for Linux /sys filesystem
      Parameters:
      filename - The file to read
      Returns:
      The value contained in the file, if any; otherwise zero
    • getUnsignedLongFromFile

      public static long getUnsignedLongFromFile​(java.lang.String filename)
      Read a file and return the unsigned long value contained therein as a long. Intended primarily for Linux /sys filesystem
      Parameters:
      filename - The file to read
      Returns:
      The value contained in the file, if any; otherwise zero
    • getIntFromFile

      public static int getIntFromFile​(java.lang.String filename)
      Read a file and return the int value contained therein. Intended primarily for Linux /sys filesystem
      Parameters:
      filename - The file to read
      Returns:
      The value contained in the file, if any; otherwise zero
    • getStringFromFile

      public static java.lang.String getStringFromFile​(java.lang.String filename)
      Read a file and return the String value contained therein. Intended primarily for Linux /sys filesystem
      Parameters:
      filename - The file to read
      Returns:
      The value contained in the file, if any; otherwise empty string
    • getKeyValueMapFromFile

      public static java.util.Map<java.lang.String,​java.lang.String> getKeyValueMapFromFile​(java.lang.String filename, java.lang.String separator)
      Read a file and return a map of string keys to string values contained therein. Intended primarily for Linux /proc/[pid] files to provide more detailed or accurate information not available in the API.
      Parameters:
      filename - The file to read
      separator - Character(s) in each line of the file that separate the key and the value.
      Returns:
      The map contained in the file, delimited by the separator, with the value whitespace trimmed. If keys and values are not parsed, an empty map is returned.
    • readPropertiesFromFilename

      public static java.util.Properties readPropertiesFromFilename​(java.lang.String propsFilename)
      Read a configuration file from the sequence of context classloader, system classloader and classloader of the current class, and return its properties
      Parameters:
      propsFilename - The filename
      Returns:
      A Properties object containing the properties.
    • readSymlinkTarget

      public static java.lang.String readSymlinkTarget​(java.io.File file)
      Reads the target of a symbolic link
      Parameters:
      file - The file to read
      Returns:
      The symlink name, or null if the read failed