如何阅读与BufferedReader类文件的第一行? [英] How to Read 1st line of a file with BufferedReader?
问题描述
我和试验的BufferedReader读第1行文件的字符串。我该怎么做呢?此外,我怎么能读整个文件到一个字符串?如何阅读像的ReadLine(INT线)一个特定的行
不通过previous线迭代?
文件namefile =新的文件(根。名称);
的FileReader namereader =新的FileReader(namefile);
在的BufferedReader =新的BufferedReader(namereader);
您可以使用的 BufferedReader.readLine()
拿到的第一行。
请注意,下次调用的readLine()
将让你的二号线,而接下来的3号线......
编辑:
结果如果要指定一个特定的线路,因为您的评论建议 - 你可能想使用Apache共享文件实用程序,以及使用:<一href=\"http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html#readLines%28java.io.File%29\"相对=nofollow> FileUtils.readLines()
。这将让你一个 列表与LT;弦乐&GT;
,你可以处理像任何名单,其中包括获得特定行。请注意,它有更多的开销,因为它读取整个文件,并填充一个列表与LT;弦乐方式&gt;
,其行
I was experimenting with BufferedReader to read 1st line file to a string. How do I do this? Also how can I read an entire file to a string? How to read a particular line like readline(int line)
without iterating through the previous lines?
File namefile = new File(root, ".name");
FileReader namereader = new FileReader(namefile);
BufferedReader in = new BufferedReader(namereader);
You can use BufferedReader.readLine()
to get the first line.
Note that the next call to readLine()
will get you the 2nd line, and the next the 3rd line....
EDIT:
If you want to specify a specific line, as your comment suggest - you might want to use Apache Commons FileUtils, and use: FileUtils.readLines()
. It will get you a List<String>
which you can handle like any list, including getting a specific line. Note that it has more overhead because it reads the entire file, and populates a List<String>
with its lines.
这篇关于如何阅读与BufferedReader类文件的第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!