java 使用BufferedReader读取文本文件

rt4zxlrg  于 2022-12-25  发布在  Java
关注(0)|答案(9)|浏览(147)

我在使用BufferedReader时遇到问题
我想打印一个文本文件的6行:

public class Reader {

public static void main(String[]args) throws IOException{
    
    FileReader in = new FileReader("C:/test.txt");
    BufferedReader br = new BufferedReader(in);
    
    while (br.readLine() != null) {
        System.out.println(br.readLine());
    }
    in.close();
    
}

现在,根据我每次调用readLine()方法时收集到的信息,它会自动前进到下一行。
所以我不能使用条件br.readLine()!= null,因为它已经向前推进了一行,我得到了输出:

Line 2
Line 4
Line 6

我应该使用什么条件来检查文本字段中是否还有新行?

jogvjijk

jogvjijk1#

这就是问题所在:

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

您有两个对readLine的调用-第一个 only 检查是否有一行(但读取后丢弃),第二个读取 next 行。

String line;
while ((line = br.readLine()) != null) {
    System.out.println(line);
}

现在我们只在每个循环迭代中调用readLine()once,并使用我们在“我们完成了吗?”和“打印出该行”部分中读取的行。

2ic8powd

2ic8powd2#

你通过while循环读取line,然后通过这个循环读取下一行,所以只需要在while循环中读取它

String s;
 while ((s=br.readLine()) != null) {
     System.out.println(s);
  }
xmd2e60i

xmd2e60i3#

可以将br.readLine()的结果赋给一个变量,并使用它进行处理和检查,如下所示:

String line = br.readLine();
while (line != null) { // You might also want to check for empty?
    System.out.println(line);
    line = br.readLine();
}
rhfm7lfc

rhfm7lfc4#

对资源使用try。这将自动关闭资源。

try (BufferedReader br = new BufferedReader(new FileReader("C:/test.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
} catch (Exception e) {

}
qvk1mo1f

qvk1mo1f5#

private void readFile() throws Exception {
      AsynchronousFileChannel input=AsynchronousFileChannel.open(Paths.get("E:/dicom_server_storage/abc.txt"),StandardOpenOption.READ);
      ByteBuffer buffer=ByteBuffer.allocate(1024);
      input.read(buffer,0,null,new CompletionHandler<Integer,Void>(){
        @Override public void completed(    Integer result,    Void attachment){
          System.out.println("Done reading the file.");
        }
        @Override public void failed(    Throwable exc,    Void attachment){
          System.err.println("An error occured:" + exc.getMessage());
        }
      }
    );
      System.out.println("This thread keeps on running");
      Thread.sleep(100);
    }
vecaoik1

vecaoik16#

也许你是指这个

public class Reader {

public static void main(String[]args) throws IOException{

    FileReader in = new FileReader("C:/test.txt");
    BufferedReader br = new BufferedReader(in);
    String line = br.readLine();
    while (line!=null) {
        System.out.println(line);
        line = br.readLine();
    }
    in.close();

}
0h4hbjxa

0h4hbjxa7#

试试看:

String text= br.readLine();
while (text != null) 
{
  System.out.println(text);
  text=br.readLine();
}
in.close();
vxbzzdmp

vxbzzdmp8#

public String getFileStream(final String inputFile) {
        String result = "";
        Scanner s = null;

        try {
            s = new Scanner(new BufferedReader(new FileReader(inputFile)));
            while (s.hasNext()) {
                result = result + s.nextLine();
            }
        } catch (final IOException ex) {
            ex.printStackTrace();
        } finally {
            if (s != null) {
                s.close();
            }
        }
        return result;
}

这也是第一行。

htrmnn0y

htrmnn0y9#

你可以把它存储在数组中,然后使用你想要的任何一行。这是我用来从文件中读取行并将其存储在字符串数组中的代码片段,希望这对你有用:)

public class user {
 public static void main(String x[]) throws IOException{
  BufferedReader b=new BufferedReader(new FileReader("<path to file>"));
  String[] user=new String[30];
  String line="";
  while ((line = b.readLine()) != null) {
   user[i]=line; 
   System.out.println(user[1]);
   i++;  
   }

 }
}

相关问题