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

2015/4/1

Help - storing scores in a text file

Leenza Leenza

2015/4/1

#
I want to create a text file that stores the score of every person that plays my game. However, I have no idea how to do this. I know somehow in should probably involve try/ catch and streams but other than that I'm completely lost. Any suggestions would be greatly appreciated.
danpost danpost

2015/4/1

#
You will not be able to write to any files with any scenario uploaded on this site (for security reasons). Unfortunately, with the tightened security of java 8, even the UserInfo class provided by greenfoot (which was to allow score saving by users) is ?? temporarily ?? not writable.
davmac davmac

2015/4/2

#
danpost, there is the option of editing your security policy if you are desperate to have UserInfo working (we are still working on a better solution, but ultimately, it is getting harder and harder to do anything non-trivial with Java applets). Leenza: as danpost says, writing files won't work if you upload scenarios here, but it's possible for scenarios that you run on your own PC. In that case, it seems like you are on the right track already; you open a stream or writer to a file (eg FileOutputStream or FileWriter - i'd suggest that using a Writer may be simpler), you then use the methods available on the stream/writer to write the values that you want to write (i.e. the scores and any other information). Yes, you'll probably need to use try ... catch to handle exceptions. I could break it down a little as:
  • Create a FileWriter
  • write your data
  • close the writer
If you need more help I think you need to ask some more specific questions. Try your best to write some code (even if you have to leave gaps) and post it.
fejfo fejfo

2015/4/3

#
in the begining I had a lot of trouble with this to but I made an IOhelp class and now I understand it you can take a look at it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import java.io.*;
import java.util.*;
import greenfoot.*;
 
public class IoHelp implements Serializable
{
    String main,save,Class,object,image,objectX,objectY,objectRotation;
 
    public IoHelp() {
        makeSaveFolders();
    }  
 
    public void makeSaveFolders() {
        main = "C:/Users/Jef/AppData/Roaming/.MinecaftAutoPlayer1.8 file/";
        save = main + "save/";
        //Class = main + "class/";
        //object = main + "object/";
        image = main + "image/";
        //objectX = object + "x/";
        //objectY = object + "y/";
        //objectRotation = object + "rotation/";
        makeFolder(main);
        makeFolder(save);
        //makeFolder(Class);
        //makeFolder(object);
        makeFolder(image);
        //makeFolder(objectX);
        //makeFolder(objectY);
        //makeFolder(objectRotation);
    }
 
    public void makeFolder(String folderName) {
        File folder = new File( folderName );           
        folder.mkdir();
        //folder.close();
    }
 
    public void saveFile( ArrayList<String> lines, String fileName )
    {
        try  {
            File file = new File(fileName);
            //file.mkdir();
            file.createNewFile();
 
            PrintWriter out = new PrintWriter(file);
            for(String text: lines) {
                out.println(text);
            }
            out.close();
            //println("Done writing to " + fileName); //For testing
        }
        catch(IOException e)
        {
            System.out.println("IoHelp: saveFile Error: " + e);
            e.printStackTrace( );
            //out.close();
        }
    }
     
    public void println(String line) {
        TerminalWindow.active.addLine(line);
    }
 
    public ArrayList<String> loadFile(String fileName) {       
        ArrayList<String> list = new ArrayList<String>();
        try {
            File file = new File( fileName );
            Scanner in = new Scanner(file);
            try {
                while(true) {
                    list.add(in.nextLine());
                }               
            } catch(NoSuchElementException e) {
                in.close();
            }
        } catch(IOException ex) {
            println("IoHelp: loadFile ERROR: " + ex + "making file");
            try {
                File file = new File(fileName);
                file.createNewFile();
            } catch (IOException e) {
                println("IoHelp: saveFile Error: " + e);
                e.printStackTrace( );
            }
        }
 
        return list;
    }
 
    //Greenfoot only
    public void saveActor(java.util.ArrayList<greenfoot.Actor> objects,String fileName) {
        //saving object
        try {           
            FileOutputStream file = new FileOutputStream(object+fileName);
            ObjectOutputStream out = new ObjectOutputStream(file);
            for(Actor o: objects) {
                out.writeObject(o);
            }
            out.close();
        } catch(IOException ex) {
            println("IoHelp: saveActor ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //out.close();
        }
        //saving lockation
        //make arrays with all the lockations and rotation
        ArrayList<String> x = new ArrayList();
        ArrayList<String> y = new ArrayList();
        ArrayList<String> ro = new ArrayList();
        for(Actor o: objects) {
            if(o.getWorld() != null) {
                x.add(Integer.toString(o.getX()));
                y.add(Integer.toString(o.getY()));
                ro.add(Integer.toString(o.getRotation()));
            } else {
                x.add("not in world");
                y.add("not in world");
                ro.add(Integer.toString(0));
            }
        
        //save lockations
        saveFile(x,objectX+fileName);
        saveFile(y,objectY+fileName);
        saveFile(ro,objectRotation+fileName);
    }
 
    public void saveWorld(ArrayList<World> objects,String fileName) {
        try {
            FileOutputStream file = new FileOutputStream(fileName);
            ObjectOutputStream out = new ObjectOutputStream(file);
            for(World o: objects) {
                out.writeObject(o);
            }
            out.close();
        } catch(IOException ex) {
            println("IoHelp: saveWorld ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //out.close();
        }
    }
 
    public ArrayList<Actor> loadActor(String fileName,World caller) {
        ArrayList<Actor> list = new ArrayList<Actor>();
        try {
            FileInputStream file = new FileInputStream(object + fileName);
            ObjectInputStream in = new ObjectInputStream(file);
            try {
                while(true) {
                    try {
                        list.add((Actor) in.readObject());
                    } catch(ClassNotFoundException cnfex) {
                        println("IoHelp: load Actor class ERROR: " + cnfex);
                    }
                }
            } catch(java.io.EOFException e) {
                //ignore this
            }    
            in.close();
        } catch(IOException ex) {
            if(ex.toString() == "java.io.EOFException") {
                //ingnore it
            }else {
                println("IoHelp: load Actor file ERROR: " + ex);
                try {
                    File file = new File(object + fileName);
                    file.createNewFile();
                } catch (IOException e) {
                    println("IoHelp: saveFile Error: " + e);
                    e.printStackTrace( );
                
            }
        }
        //got the actors adding them to the world if they were in the world
        ArrayList<String> textX = loadFile(objectX + fileName);
        ArrayList<String> textY = loadFile(objectY + fileName);
        ArrayList<String> textRo = loadFile(objectRotation + fileName);
        for(int i = 0; i < list.size(); i++) {
            Actor o = list.get(i);
            try {
                int x = Integer.parseInt(textX.get(i));
                int y = Integer.parseInt(textY.get(i));
                int ro = Integer.parseInt(textRo.get(i));
                caller.addObject(o,x,y);
                o.setRotation(ro);
            } catch(NumberFormatException NFEx) {
                //println("object: " + " wasn't in world");
            }
        }
 
        return list;       
    }
 
    public ArrayList<World> loadWorld(String fileName) {
        ArrayList<World> list = new ArrayList<World>();
        try {
            FileInputStream file = new FileInputStream(fileName);
            ObjectInputStream in = new ObjectInputStream(file);
            try {
                while(true) {
                    try {
                        list.add((World) in.readObject());
                    } catch(ClassNotFoundException cnfex) {
                        println("IoHelp: load World class ERROR: " + cnfex);
                    }
                }
            } catch(EOFException e) {
                //ignore this
            }
            in.close();
        } catch(IOException ex) {
            println("IoHelp: load World file ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //in.close();
        }
        return list;
    }
}
You need to login to post a reply.