简体   繁体   English

如何从Java文件中读取一行

[英]How to read a single line from file in java

How can i read single line from a text file in java. 如何从Java中的文本文件读取单行。 and what is the criteria of knowing that line is completed. 知道这条线完成的标准是什么。

secondly 其次

i read file and then for Read Line function and converting it into string will skip a lot of data? 我读取文件,然后读取行功能并将其转换为字符串会跳过很多数据? what should be the problem? 应该是什么问题? Here is my code 这是我的代码

String data = new String();
    while(infile.readLine() != null) {
    data = infile.readLine();
    System.out.println(data);
}

Change your code as follows 如下更改代码

  while((data = infile.readLine()) != null) {  // read and store only line    
  System.out.println(data);
  }

In your current code 在您当前的代码中

   while(infile.readLine() != null) { // here you are reading one and only line
   data = infile.readLine(); // there is no more line to read
    System.out.println(data);
   }

You are reading an extra line because the first readLine() as the while condition reads a line but it is used at all. 您正在读取额外的一行,因为作为while条件的第一个readLine()会读取一行,但实际上已使用了它。 The second readLine() inside the while loop read the second line which you're assigning to data and printing. while循环内的第二行readLine()读取要分配给data和打印的第二行。

Therefore you need to assign the line read in the while condition to data and print it, as that is the first line. 因此,您需要将while条件中读取的行分配给data并打印,因为这是第一行。

while((data = infile.readLine()) != null) { // reads the first line
    // data = infile.readLine(); // Not needed, as it reads the second line
    System.out.println(data); // print the first line
}

Also, since you just need to read the first line, you don't need the while at all. 此外,由于你只需要读第一行,你并不需要while在所有。 A simple if would do. 一个简单的if会做。

if((data = infile.readLine()) != null) { // reads the first line
    System.out.println(data); // print the first line
}

With the BufferedReader and the code you posted in the comments, your main should now look like this. 有了BufferedReader和您在注释中发布的代码,您的主体现在应该看起来像这样。

public static void main(String[] args) {
    try {
        FileInputStream fstream = new FileInputStream(args[0]);
        BufferedReader infile = new BufferedReader(new InputStreamReader(
                fstream));
        String data = new String();
        while ((data = infile.readLine()) != null) { // use if for reading just 1 line
            System.out.println(data);
        }
    } catch (IOException e) {
        // Error
    }
}

First thing : readLine() returns String value only so it is not converting to String. 第一件事: readLine()仅返回String值,因此不会转换为String。

Second thing : In your while loop, you read firstline and check whether the content of first line is null or not. 第二件事:在while循环中,您读取第一行并检查第一行的内容是否为null。 But when data = infile.readLine(); 但是当data = infile.readLine(); executes, it will fetch second line from file and print it to Console. 执行,它将从文件中获取第二行并将其打印到控制台。

Change your while loop to this : 将您的while循环更改为:

while((data = infile.readLine()) != null){ 
    System.out.println(data); 
}

If you use toString() method, it will throw NPE when it will try to use toString method with null content read from infile . 如果使用toString()方法,则当它尝试使用toString方法并从infile读取null内容时,它将抛出NPE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM