0

我有一个包含以下 URL 的文本文件:

http://www.google.com.uy
http://www.google.com.es
http://www.google.com.nz

我需要阅读此 TXT 的第二行,那里显示了第二个 URL。我一直在研究,并没有找到我真正需要的东西,因为虽然我知道我必须使用BufferedReader该类,但我不知道如何指定我想要阅读的“行”。

这是我到目前为止写的:

String fileread = "chlist\\GATHER.txt";
try {                                         
    BufferedReader br = new BufferedReader(new FileReader(fileread));
    String gatherText = br.readLine();
    br.close();
} catch (IOException ioe) {}
4

5 回答 5

1

Each call to br.readLine(); will return you a line from the text file and move you to the next line, so the second call will give you the line you want. The simplest way would be to just write:

String fileread = "chlist\\GATHER.txt";
    try {                                         
        BufferedReader br = new BufferedReader(new FileReader(fileread));
        br.readLine();
        String gatherText = br.readLine();
        br.close();
} catch (IOException ioe) {}

Although you should also consider what you would do if the file does not contain two lines.

于 2013-09-04T14:35:59.083 回答
0

使用它,您可以按每一行获取文件数据

导入 org.apache.commons.io.FileUtils;使用 apache-common-io 工具

try
{
    List<String> stringList = FileUtils.readLines(new File("chlist\\GATHER.txt"));
    for (String string : stringList)
    {
        System.out.println("Line String : " + string);
    }
}
catch (IOException ex)
{
    Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
于 2013-09-04T14:32:52.410 回答
0
String fileread = "chlist\\GATHER.txt";
try {                                         
BufferedReader br = new BufferedReader(new FileReader(fileread));
String gatherText;
int counter = 0;
while((gatherText = br.readLine()) != null) {
  if(counter ++ == 1) {
    // This is the line you wanted in your example
    System.out.println(gatherText);
  }
}
br.close();
} catch (IOException ioe) {}
于 2013-09-04T14:36:21.943 回答
0

您可以使用

Apache Commons IO 

然后下面的代码

 String line = FileUtils.readLines(file).get(2);
于 2013-09-04T14:38:09.940 回答
0

尝试保留一个计数器:

final int linesToSkip = 1;
for(int i=0; i<linesToSkip; i++) br.readLine();
String gatherText = br.readLine();
于 2013-09-04T14:33:11.007 回答