Thursday, August 14, 2014

Read text file using Stream

This example show how to read text file using Stream<String> by calling Files.lines(path); to read the file /proc/cpuinfo, /proc/version or /proc/meminfo or Linux.

package java_streamreadtextfile;

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_StreamReadTextFile {

    //final static String FILE_NAME = "/proc/cpuinfo";
    //final static String FILE_NAME = "/proc/version";
    final static String FILE_NAME = "/proc/meminfo";

    public static void main(String[] args) {
        Path path = Paths.get(FILE_NAME);
        try {
            Stream<String> fileLines = Files.lines(path);
            fileLines.forEach((line) -> System.out.println(line));

        } catch (IOException ex) {
            Logger.getLogger(Java_StreamReadTextFile.class.getName()).log(Level.SEVERE, null, ex);

- Parse /proc/meminfo using Java code

1 comment: