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
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.