Package oshi.util
Class FileUtil
- java.lang.Object
-
- oshi.util.FileUtil
-
@ThreadSafe public final class FileUtil extends java.lang.Object
File reading methods
-
-
Field Summary
Fields Modifier and Type Field Description private static int
BUFFER_SIZE
private static org.slf4j.Logger
LOG
private static java.lang.String
READ_LOG
private static java.lang.String
READING_LOG
-
Constructor Summary
Constructors Modifier Constructor Description private
FileUtil()
-
Method Summary
All Methods Static Methods Concrete Methods 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 byte[]
readAllBytes(java.lang.String filename, boolean reportError)
Read an entire file at one time.static java.nio.ByteBuffer
readAllBytesAsBuffer(java.lang.String filename)
Read an entire file at one time.static void
readByteArrayFromBuffer(java.nio.ByteBuffer buff, byte[] array)
Reads a byte array value from a ByteBufferstatic byte
readByteFromBuffer(java.nio.ByteBuffer buff)
Reads a byte value from a ByteBufferstatic 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 byte[]
readFileAsBytes(java.net.URL url)
Reads a URL into a byte arraystatic int
readIntFromBuffer(java.nio.ByteBuffer buff)
Reads an int value from a ByteBufferstatic java.util.List<java.lang.String>
readLines(java.lang.String filename, int count)
Read count lines from a file.static java.util.List<java.lang.String>
readLines(java.lang.String filename, int count, boolean reportError)
Read count lines from a file.static long
readLongFromBuffer(java.nio.ByteBuffer buff)
Reads a long value from a ByteBufferstatic com.sun.jna.NativeLong
readNativeLongFromBuffer(java.nio.ByteBuffer buff)
Reads a NativeLong value from a ByteBufferstatic com.sun.jna.Pointer
readPointerFromBuffer(java.nio.ByteBuffer buff)
Reads a Pointer value from a ByteBufferprivate static boolean
readPropertiesFromClassLoader(java.lang.String propsFilename, java.util.Properties archProps, java.lang.ClassLoader loader)
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 propertiesstatic short
readShortFromBuffer(java.nio.ByteBuffer buff)
Reads a short value from a ByteBufferstatic com.sun.jna.platform.unix.LibCAPI.size_t
readSizeTFromBuffer(java.nio.ByteBuffer buff)
Reads a size_t value from a ByteBufferstatic java.lang.String
readSymlinkTarget(java.io.File file)
Reads the target of a symbolic link
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
-
READING_LOG
private static final java.lang.String READING_LOG
- See Also:
- Constant Field Values
-
READ_LOG
private static final java.lang.String READ_LOG
- See Also:
- Constant Field Values
-
BUFFER_SIZE
private static final int BUFFER_SIZE
- See Also:
- Constant Field Values
-
-
Method Detail
-
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 readreportError
- 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
-
readLines
public static java.util.List<java.lang.String> readLines(java.lang.String filename, int count)
Read count lines from a file. Intended primarily for Linux /proc filesystem to avoid recalculating file contents on iterative reads.- Parameters:
filename
- The file to readcount
- The number of lines to read- Returns:
- A list of Strings representing the first count lines of the file, or an empty list if file could not be read or is empty
-
readLines
public static java.util.List<java.lang.String> readLines(java.lang.String filename, int count, boolean reportError)
Read count lines from a file. Intended primarily for Linux /proc filesystem to avoid recalculating file contents on iterative reads.- Parameters:
filename
- The file to readcount
- The number of lines to readreportError
- Whether to log errors reading the file- Returns:
- A list of Strings representing the first count lines of the file, or an empty list if file could not be read or is empty
-
readAllBytes
public static byte[] readAllBytes(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 readreportError
- Whether to log errors reading the file- Returns:
- A byte array representing the file
-
readAllBytesAsBuffer
public static java.nio.ByteBuffer readAllBytesAsBuffer(java.lang.String filename)
Read an entire file at one time. Intended for unix /proc binary files to avoid reading file contents on iterative reads.- Parameters:
filename
- The file to read- Returns:
- A bytebuffer representing the file if read was successful; null otherwise
-
readByteFromBuffer
public static byte readByteFromBuffer(java.nio.ByteBuffer buff)
Reads a byte value from a ByteBuffer- Parameters:
buff
- The bytebuffer to read from- Returns:
- The next byte value
-
readShortFromBuffer
public static short readShortFromBuffer(java.nio.ByteBuffer buff)
Reads a short value from a ByteBuffer- Parameters:
buff
- The bytebuffer to read from- Returns:
- The next short value
-
readIntFromBuffer
public static int readIntFromBuffer(java.nio.ByteBuffer buff)
Reads an int value from a ByteBuffer- Parameters:
buff
- The bytebuffer to read from- Returns:
- The next int value
-
readLongFromBuffer
public static long readLongFromBuffer(java.nio.ByteBuffer buff)
Reads a long value from a ByteBuffer- Parameters:
buff
- The bytebuffer to read from- Returns:
- The next long value
-
readNativeLongFromBuffer
public static com.sun.jna.NativeLong readNativeLongFromBuffer(java.nio.ByteBuffer buff)
Reads a NativeLong value from a ByteBuffer- Parameters:
buff
- The bytebuffer to read from- Returns:
- The next value
-
readSizeTFromBuffer
public static com.sun.jna.platform.unix.LibCAPI.size_t readSizeTFromBuffer(java.nio.ByteBuffer buff)
Reads a size_t value from a ByteBuffer- Parameters:
buff
- The bytebuffer to read from- Returns:
- The next value
-
readByteArrayFromBuffer
public static void readByteArrayFromBuffer(java.nio.ByteBuffer buff, byte[] array)
Reads a byte array value from a ByteBuffer- Parameters:
buff
- The bytebuffer to read fromarray
- The array into which to read the data
-
readPointerFromBuffer
public static com.sun.jna.Pointer readPointerFromBuffer(java.nio.ByteBuffer buff)
Reads a Pointer value from a ByteBuffer- Parameters:
buff
- The bytebuffer to read from- Returns:
- The next value
-
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 readseparator
- 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.
-
readPropertiesFromClassLoader
private static boolean readPropertiesFromClassLoader(java.lang.String propsFilename, java.util.Properties archProps, java.lang.ClassLoader loader)
-
readFileAsBytes
public static byte[] readFileAsBytes(java.net.URL url) throws java.io.IOException
Reads a URL into a byte array- Parameters:
url
- The URL of the file to read- Returns:
- the byte content of the file
- Throws:
java.io.IOException
- on file reading failure
-
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
-
-