我有一个包含 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(); }
}}