使用java从文本文件中读取数据行
我有一个包含 x 行数的文本文件。每行保存一个整数。当用户单击按钮时,将通过 actionlistener 执行操作,其中应列出文本文件中显示的所有值。然而,现在我将 linenum 设置为 10,这意味着我已经告诉代码只处理文本文件的 10 行。因此,如果我的文本文件只有 3 行/行数据...它将列出这些行,而对于其余 7 行,它将输出“null”。
我记得有一种方法可以使用省略号让程序知道您不知道确切的值,但最后它会根据给定的信息计算它。我给定的信息将是带有数字(数据)的行数。
下面是部分代码。
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
BufferedReader inputFile=null;
try {
FileReader freader =new FileReader("Data.txt");
inputFile = new BufferedReader(freader);
String MAP = "";
int linenum=10;
while(linenum > 0)
{
linenum=linenum-1;
MAP = inputFile.readLine();//read the next line until the specfic line is found
System.out.println(MAP);
}
} catch( Exception y ) { y.printStackTrace(); }
}}
I have a text file with x amount of lines. each line holds a integer number. When the user clicks a button, an action is performed via actionlistener where it should list all the values as displayed on the text file. However, right now I have linenum set to 10 implying I already told the code that just work with 10 lines of the text file. So, if my text file has only 3 rows/lines of data...it will list those lines and for rest of the other 7 lines, it will spit out "null".
I recall there is a way to use ellipsis to let the program know that you don't know the exact value but at the end it calculates it based on the given information. Where my given information will the number of lines with numbers(data).
Below is part of the code.
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
BufferedReader inputFile=null;
try {
FileReader freader =new FileReader("Data.txt");
inputFile = new BufferedReader(freader);
String MAP = "";
int linenum=10;
while(linenum > 0)
{
linenum=linenum-1;
MAP = inputFile.readLine();//read the next line until the specfic line is found
System.out.println(MAP);
}
} catch( Exception y ) { y.printStackTrace(); }
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需将
linenum > 代替即可0
下一行(MAP = inputFile.readLine()) != null
并删除接下来的行,
linenum=linenum-1;
下次谷歌搜索可能会有所帮助+)MAP = inputFile.readLine();
最后一行的空值不会被打印,因为它将该行设置为当前行并将其与空值进行比较,因此如果最后一行为空,则不会打印它,那么 10 行限制又如何呢?你可以很容易地做到这一点,你只需将一个索引添加到 for 循环并索引并使用 && 进行检查即可。如果 i 小于 10
just put instead of
linenum > 0
the next line(MAP = inputFile.readLine()) != null
and delete the next lines,
linenum=linenum-1;
and next time a bit of googling might help +)MAP = inputFile.readLine();
The null value of the last line would not be printed because it sets the line to be the current line and compares it with the null value so if the last line is null it will not print it and what about the 10 lines limit? you can do it easily you can just add an an index to the for loop and to index and to check with && if the i is lower then 10
测试从 BufferedReader.readLine() 返回的值,如果为空则停止循环,如下所示:
编辑:忘记了获取前 10 行的要求,您可以更改上面的代码以将输出放入列表中并返回列表,那么你可以通过这样的函数过滤它:
如果文件很大,那么这当然会效率低下,如果这很重要,你最终会做类似的事情:
这更丑陋,但只读取你需要的行。
Test the value that comes back from BufferedReader.readLine(), if it is null stop looping, like so:
EDIT: forgot the requirement to take first 10 lines, you can change above code to put output in a list and return the list, then you can filter it through a function like this:
If the file is huge then this will be inefficient, of course, and if that matters you will end up doing something like:
which is uglier but reads only the lines you need.
如果 MAP 的值为空,那么不打印 MAP 怎么样?
How about you don't print MAP if its value is null?