我的程序没有按预期读取文件
我正在尝试创建一个 int 值,该值每秒增加一个计数器,然后当您关闭程序时,它将将该值保存到 .txt 文件中,然后,当您启动时,它应该再次程序,从文件中读取 int 值,然后再次从该值继续,但是,它保存正确,它生成了文件,但是当我再次启动程序时,它只会再次从 1 开始,而不是从保存在 .txt 文件中的值。
int 值:
public int points;
getter 和 setter,位于 MrStan.class
public int getPoints(){
return points;
}
public void setPoints( int points ){
this.points = points;
}
我如何读取文件:
MrStanReadFile r = new MrStanReadFile();
r.openFile();
r.readFile();
r.closeFile();
My ReadFile 类:
public class MrStanReadFile {
private Scanner x;
MrStan a = new MrStan();
public void openFile(){
try{
x = new Scanner(new File("D:/MrStan/text.txt"));
}catch(Exception ex){
System.out.println("COULD NOT FIND TEXT.TXT");
}
}
public void readFile(){
while(x.hasNext()){
String p = x.next();
int pointset = Integer.parseInt(p);
a.setPoints(pointset);
}
}
public void closeFile(){
x.close();
}
}
其他使用 int 的地方:
Todotask:
public MrStan(){
timer.schedule(new todoTask(), 0, 1 * 1000);
}
class todoTask extends TimerTask{
public void run(){
points++;
repaint();
}
}
private Timer timer = new Timer();
好吧
g.drawString("Points: " + points, 75, 83);
,在 readFile 方法中,您将看到一个字符串, p,这是文本文件中的字符串。我使用 Integer.parseInt() 将字符串转换为 int,然后,我将 int 值设置为转换后的点集值,但它没有改变,我的程序将如何工作,以便它将从中设置的数字启动.txt 文件?
I'm trying to make a int value, which goes up by a counter every second, then when you close the program down, it will the save the value to a .txt file, and then, it SHOULD, when you start up the program again, read the int value from the file, and continue from that value again, however, it is saving correctly, it makes the file, but when I startup my program again, it will just start at 1 again, and not at the value which was saved in the .txt file.
The int value:
public int points;
The getter and setter, located in MrStan.class
public int getPoints(){
return points;
}
public void setPoints( int points ){
this.points = points;
}
How i'm reading the file:
MrStanReadFile r = new MrStanReadFile();
r.openFile();
r.readFile();
r.closeFile();
My ReadFile class:
public class MrStanReadFile {
private Scanner x;
MrStan a = new MrStan();
public void openFile(){
try{
x = new Scanner(new File("D:/MrStan/text.txt"));
}catch(Exception ex){
System.out.println("COULD NOT FIND TEXT.TXT");
}
}
public void readFile(){
while(x.hasNext()){
String p = x.next();
int pointset = Integer.parseInt(p);
a.setPoints(pointset);
}
}
public void closeFile(){
x.close();
}
}
Other places where the int is used:
Todotask:
public MrStan(){
timer.schedule(new todoTask(), 0, 1 * 1000);
}
class todoTask extends TimerTask{
public void run(){
points++;
repaint();
}
}
private Timer timer = new Timer();
and
g.drawString("Points: " + points, 75, 83);
Okay, at the readFile method, you will see a string, p, that's the string in the text file. I convert the string to an int, using Integer.parseInt(), then, I set my int value to the converted pointset value, but it isn't changing, how will my program work so it will start up from the number set in the .txt file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“COULD NOT FIND TEXT.TXT”
?int pointset = Integer.parseInt(p);
之后添加System.out.println("Read: "+ pointset);
this.points = 0;
任何其他地方。MrStanReadFile
内定义MrStan a = new MrStan();
。您确定它与您在class todoTask
中使用的对象相同吗?"COULD NOT FIND TEXT.TXT"
?System.out.println("Read: "+ pointset);
right afterint pointset = Integer.parseInt(p);
this.points = 0;
anywhere else.MrStan a = new MrStan();
insideMrStanReadFile
. Are you sure it's the same object as the one you use inclass todoTask
?