我想读取我的highscore.txt文件,以便我可以在我的游戏的高分菜单上显示它。 我的highscore.txt的内容是SCORE和NAME。例如:Java文件读取问题
150 John
100 Charice
10 May
所以我写了下面的代码读取文本区域中的文件,但该文件无法读取。我的代码如下:
public HighScores(JFrame owner) {
super(owner, true);
initComponents();
setSize(500, 500);
setLocation(300, 120);
getContentPane().setBackground(Color.getHSBColor(204, 204, 255));
File fIn = new File("highscores.txt");
if (!fIn.exists()) {
jTextArea1.setText("No high scores recorded!");
} else {
ArrayList v = new ArrayList();
try {
BufferedReader br = new BufferedReader(new FileReader(fIn));
String s = br.readLine();
while (s != null) {
s = s.substring(2);
v.add(s);
s = br.readLine();
}
br.close();
} catch (IOException ioe) {
JOptionPane.showMessageDialog(null, "Couldn't read high scores file");
}
// list to display high scores
JList jlScores = new JList(v); //there's a problem here. I don't know why
jTextArea1.add(jlScores, BorderLayout.CENTER);
}
}
我在做什么错???我怎样才能做这件事>。你的帮助将不胜感激。先谢谢你。
你可能想看看你是如何从你的文件解析文本行。 `s = s.substring(2);`只是从字符串中删除前两个字符。你可能会想要在空间字符串上拆分字符串以获得一个令牌数组;在这种情况下,[0]将是得分,[1]将是名称。 – Qwerky 2011-01-31 16:46:20
@Qwerky我怎么做分裂?谢谢 – newbie 2011-01-31 16:47:36