This site requires JavaScript, please enable it in your browser!
Greenfoot back
egenius
egenius wrote ...

2011/3/13

Save best point

1
2
egenius egenius

2011/3/13

#
Hi, everybody! Yesterday I made a game http://greenfootgallery.org/scenarios/2609 . I made a txt file that saves best point. And if you bet the record then it updates. It works in greenfoot excellent, but when I export it on the dekstop, for some reason it doesn't work, it can't find the txt file (record.txt), but it's there, I can see it! ))
davmac davmac

2011/3/15

#
Exactly how are you opening the txt file?
egenius egenius

2011/3/16

#
Here is my code: import greenfoot.*; import java.io.*; import java.lang.*; import java.util.*; public class BestScore extends Actor { private Formatter x; private Scanner y; private int best; int number; public BestScore(int number) { this.number = number; } public void OpenFile(){ try { x = new Formatter("record.txt"); y = new Scanner (new File("record.txt") ); } catch (Exception e){ System.out.println("You have an error!"); } } public void addRecords(){ if (number>best) { x.format("%s", number); } } public void closeFile(){ x.close(); } public void readFile() { String s="0"; while(y.hasNext() ) { s = y.next(); } best = Integer.parseInt(s); } }
davmac davmac

2011/3/17

#
Well: new File("record.txt") opens a file which must exist in the "current" directory. That could be pretty much any directory if you have exported your application, though it depends on how you run it. Where is the record.txt file? Does it exist in the same directory as the .jar file? How are you running the jar file?
egenius egenius

2011/3/17

#
It exists in the same directory as jar file. How I run the jar file: right click on the file, then open with Java(TM) Platform SE binary
davmac davmac

2011/3/18

#
Ok: how do you know that the problem is that it can't find the file? From the code snippet you've posted, there's no obvious error message output except to System.out which you won't see if you run the jar via the GUI.
egenius egenius

2011/3/18

#
I'm not sure that it can't find the file, but I think so, because by in record.txt best point = 120, and in my program best point = 0. And however it appears 0 on the screen : public void readFile() { String s="0"; while(y.hasNext() ) { s = y.next(); } best = Integer.parseInt(s); } And when I run it in Greenfoot, it gets best point from the record.txt and converts it to int (that's what I want) , but when I run jar file, it doesn't work normally(. Maybe I should post the source code?
davmac davmac

2011/3/21

#
From the sounds of it, the problem is really that the current directory isn't reliably set to anything in particular when you run the jar file. You could try hard-coding the full path to the file, or prompt the user for the file location.
mik mik

2011/3/21

#
You might like to look at this: http://www.bluej.org/help/archive.html#tip10
egenius egenius

2011/3/22

#
Thank you! Now I can read the file, but I don't know to write in it. And why I can't write: public InputStream openFile(String fileName) throws IOException { URL url = getClass().getClassLoader().getResource(fileName); if(url == null) throw new IOException("File not found: " + fileName); return url.toString(); } FileInputStream in = new FileInputStream(openFile("record.txt")); ?
davmac davmac

2011/3/23

#
I think that the instructions at http://www.bluej.org/help/archive.html#tip10 aren't actually helping you here - the technique described there can help you read from a file which has been included inside your jar, but you won't be able to write to it. Your problem is a bit different: you want to be able to read and write to a file which sits in the same place as the jar file. Did you try hard-coding the full path to the file, as I suggested earlier?
egenius egenius

2011/3/23

#
Yes, I did, look: x = new Formatter("D:\\Program Files\\Greenfoot\\scenarios\\appleGame.jar\\record.txt"); y = new Scanner(new File("D:\\Program Files\\Greenfoot\\scenarios\\appleGame.jar\\record.txt")); but it didn't help ( (In Greenfoot it runs normally, but the path is a bit different x = new Formatter("D:\\Program Files\\Greenfoot\\scenarios\\appleGame\\record.txt"); ) Maybe the system can not open jar file??
davmac davmac

2011/3/24

#
Ok, I'm a bit confused now. Is the record.txt file you want to open in the same *directory* as the jar file, like you said earlier, or is it actually *inside* the jar file? If the former, then the path you specified looks wrong; it should probably be "D:\\Program Files\\Greenfoot\\scenarios\\record.txt". If the latter, you can use the technique given by mik to read the file, but you cannot write to the file. There is no simple way to write to a file which is inside a jar file. And, you cannot open a file inside a jar file in the same way you can open other files; the operating system doesn't treat them the same way.
egenius egenius

2011/3/26

#
It's indside the jar file, I 've already understood that there is no way to solve my problem(, but anyway thank you!
MathManiac MathManiac

2011/4/3

#
Actually davmac, I challenge what you said earlier in the discussion. You said that the new File("BestPoint.txt"); command opens up a file if only it exsists. However, in my Head First Java book, it said that It opens the file if created, however, if it's not created, it creates a new file. However, it will be empty, so var "b" will be considered null if you do this command: String b = new BufferedReader(new FileReader(new File("TheFileNeverCreated.txt"))).readLine();
There are more replies on the next page.
1
2