java从声明了静态常量的文本文件读取
我正在尝试编写一个代码来读取文件的内容,并且我能够成功地读取它。我还想做的是声明一个静态常量,比如max_number_of_players,这样读取的数据就不会超过这个值。我有办法做到这一点吗?到目前为止,我的代码是:
import java.io.*;
import java.util.*;
public class Test{
public static void main(String[] args) throws IOException
{
String fileName = "Data/players.txt";
File file = new File(fileName);
Scanner in = new Scanner(file);
while(in.hasNextLine()){
String line = in.nextLine();
System.out.println(line);
}
in.close();
}
}
# 1 楼答案
只需在while循环中添加另一个条件: