Java - 从第二行开始读取文本文件 [英] Java - read text file starting from second line

查看:2194
本文介绍了Java - 从第二行开始读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在java中读取txt文件。但是,我只想从第二行开始读取,因为第一行只是一个标签。这是示例

I am trying to read a txt file in java. However, I only want to read starting from the second line since the first line is just a label. This is the example

文本文件:

Name,Type,Price
Apple,Fruit,3
Orange,Fruit,2
Lettuce,Veggie,1

我该怎么做?我有这个代码,您可以从第一行读取。

How do I do this? I have this code where you can read from first line.

代码:

//read the file, line by line from txt
File file = new File("train/traindata.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;

line = br.readLine();

while(line != null)
{
    lines = line.split(",");

    //Do something for line here
    //Store the data read into a variable

    line = br.readLine();         
}

fr.close();

请帮助我,提前谢谢。

推荐答案

只需添加一个额外的 BufferedReader #readLine 调用...

Just add an extra BufferedReader#readLine call...

br.readLine(); // consume first line and ignore
line = br.readLine();
while(line != null) ...

这篇关于Java - 从第二行开始读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆