Thursday, August 21, 2014

Parse /proc/meminfo using Java code

The example "Read text file using Stream" return result as String. It is hard to use. To make use of it, we always have to parse as meanful items. This example show how to parse the file "/proc/meminfo".

package java_meminfo;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;

 * @web
public class Java_meminfo {
    final static String FILE_MEMINFO = "/proc/meminfo";

    public static void main(String[] args) {

        Stream<String> streamMemInfo = readFile(FILE_MEMINFO);
        streamMemInfo.forEach((line) -> {
            //split one line by whitespace/grouped whitespaces
            String[] oneLine = line.split("\\s+");

            for (String ele : oneLine) {


    static private Stream<String> readFile(String filePath){
        Path path = Paths.get(FILE_MEMINFO);
        Stream<String> fileLines = null;
        try {
            fileLines = Files.lines(path);
        } catch (IOException ex) {
            Logger.getLogger(Java_meminfo.class.getName()).log(Level.SEVERE, null, ex);
        return fileLines;

Next post show how to display it on TableView.

No comments:

Post a Comment