尝试将Java 8与流一起使用来解析/ etc / passwd

时间:2019-03-23 00:26:23

标签: java unix stream java-stream

正在尝试使用Java 1.8的流功能在macOS Mojave 10.14.3操作系统上解析/ etc / passwd文件。

/ etc / passwd文件中的前五行如下:

nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy 
Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false

这是我的实现方式

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class User {

    String name;
    Integer uid;
    Integer gid;
    String comment;
    String home;
    String shell;

    public User(String filePath) {
        String[] items = filePath.split(":");

        if (items.length != 7) {
            throw new IllegalArgumentException("Need 7 items from file and there's only: " + items.length);
        }

        this.name = items[0];
        this.uid = Integer.parseInt(items[2]);
        this.gid = Integer.parseInt(items[3]);
        this.comment = items[4];
        this.home = items[5];
        this.shell = items[6];
    }

    // Getters and Setters omitted for brevity.


    public static void main(String[] args) {
        // Read the file
        try (Stream<String> stream = Files.lines(Paths.get("/etc/passwd")))
        {
            ArrayList<User> users = stream.map(User::new).filter(u -> !u.shell.equals("/bin/false"))
                                                         .filter(u -> !u.shell.equals("/usr/sbin/nologin"))
                                                         .collect(Collectors.toCollection(ArrayList::new));
            for (User user : users) {
                System.out.println("User: " + user.name);
            }
        } 

        catch(Throwable t) {
            t.printStackTrace();
        }   
    }
}

IllegalArgumentException:

java.lang.IllegalArgumentException: Need 7 items from file and there's only: 1
    at com.sample.model.User.<init>(User.java:23)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Iterator.forEachRemaining(Iterator.java:116)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at com.sample.model.User.main(User.java:88)

问题:

  1. 我可能做错了什么?

  2. 是否有其他方法可以代替Java 1.8的Streams功能?似乎流很难调试...

1 个答案:

答案 0 :(得分:1)

我认为您需要考虑注释行/以'#'开头的行。

所以:

ArrayList<User> users = stream.filter(s -> s.charAt(0) != '#')
                              .map(User::new)
                              .filter(u -> !u.shell.equals("/bin/false"))
                              .filter(u -> !u.shell.equals("/usr/sbin/nologin"))
                              .collect(Collectors.toCollection(ArrayList::new));

关于您的意思是:调试-是的-工具还不够好(还)。通过检查传递给User构造函数的filePath并观察它是否为注释,您可能已经发现了这一点。