I am attempting to write a program that will monitor the temperature of various hardware components.The language I chose for it is Java. Now there are two ways I could read the temperatures:a) Do a cat /sys/devices/platform/coretemp.0/hwmon/hwmon2/temp1_input in Java with Process process = Runtime.getRuntime().exec(cat /sys/devices/platform/coretemp.0/hwmon/hwmon2/temp1_input);b) Create a File pointing to /sys/devices/platform/coretemp.0/hwmon/hwmon2/temp1_input and open a BufferedReader on that file to read the temperatureThe first one would seem weird, you invoke another program that reads the file and prints the contents when you actually all ready know where the file is located.The second one makes me think, will it cause any problems when Linux kernel makes an update to that file every second and I will attempt to open a BufferedReader on it every 7 seconds for example?Looking for insight on which of these methods would be smarter.
Which way is safer to read data from /sys
shell;files;java
Option B is the preferred solution for both performance and portability reasons (see below), and I would probably read the entire contents of the file at onceString content = new Scanner(new File(filename)).useDelimiter(\Z).next();(from this StackOverflow answer)You should handle I/O errors caused by the file not being present, and reading it when the file is zero bytes (this can occur when hwmon truncates the file before writing the new value to the file, while it is very brief time it can happen).Option A ends up creating 2 subprocesses - 1st it creates a shell, then passes the command line to the process, which launches what you hope is the cat command your are expecting. If the process running your program has a weird path environment variable it may fail to run cat, or run something your not expecting. This can be mitigated by using the full path  (typically /bin/cat), this is still system dependent. Overall it is almost always better to not spawn external processes unless you have no other choice.