Cannot read first line of a file

user1285928

I want to read the content of /etc/passwd file and get some data:

public void getLinuxUsers()
    {
        try
        {
            // !!! firstl line of the file is not read
            BufferedReader in = new BufferedReader(new FileReader("/etc/passwd"));
            String str;
            str = in.readLine();
            while ((str = in.readLine()) != null)
            {
                String[] ar = str.split(":");
                String username = ar[0];
                String userID = ar[2];
                String groupID = ar[3];
                String userComment = ar[4];
                String homedir = ar[5];

                System.out.println("Usrname " + username + 
                        " user ID " + userID);
            }
            in.close();
        }
        catch (IOException e)
        {
            System.out.println("File Read Error");
        }
    }

I noticed two problems:

first line of the file is not read with root account information. I starts this way:

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

And how I can modify the code to use Java 8 NIO? I want to check first the existing of the file and then to proceed with reading the content.

Óscar López

The problem is that the first readLine() is outside the loop where the string is being processed, you should delete this:

str = in.readLine();

… Because in the next line (the one with the while) you're reassigning the str variable, that's why the first line is lost: the loop's body starts processing from the second line. Finally, to use Java nio, do something like this:

if (new File("/etc/passwd").exists()) {
    Path path = Paths.get("/etc/passwd");
    List<String> lines = Files.readAllLines(path, Charset.defaultCharset());
    for (String line : lines) {
        // loop body, same as yours
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Vuex Cannot read property of undefined

来自分类Dev

Getting scanner to read text file

来自分类Dev

First line in sub report being removed in iReport

来自分类Dev

File.read的Ruby性能

来自分类Dev

python only recognizes first line of my input

来自分类Dev

How to Read JSON File

来自分类Dev

read_file无效

来自分类Dev

Applying background to ::first-line

来自分类Dev

Replace a string which is present on first line in UNIX file

来自分类Dev

File.read返回nil

来自分类Dev

NetLogo read in file as list of characters

来自分类Dev

Ruby script to read file line by line and do if statments with puts?

来自分类Dev

Read file line by line and store values

来自分类Dev

How to read a text file, then capture and read new added line

来自分类Dev

Jenkins: Log parser Plugin Error: Cannot parse log: Can't read parsing rules file

来自分类Dev

Read will loop end of file

来自分类Dev

Hovertool Bokeh "Cannot read property"

来自分类Dev

Read line by line of a file in shell script where last line doesn't end with new line

来自分类Dev

如何用`read_line()`检查EOF?

来自分类Dev

How do I get the length of the first line in a multi line string?

来自分类Dev

Node.js TypeError: Cannot read property 'file' of undefined

来自分类Dev

awk field separator not working for first line

来自分类Dev

Cannot read property 'errors' of undefined in angular 2

来自分类Dev

Cannot read property '$options' of undefined making external js file for head options

来自分类Dev

使用line ... file指令

来自分类Dev

使用line ... file指令

来自分类Dev

SnakeYaml从String First Line转储

来自分类Dev

while read line find – 用perl更快?

来自分类Dev

ChartJS、Primeng、Gap first and end of line chart

Related 相关文章

热门标签

归档