0

EDIT:Nevermind, I'm just incredibly dumb, it is working absolutely fine, I just messed up when calling the method, I made it run only under a condition that can not be met at that point, still I qould love to know whether you would've done it like I did or would've used another way

I'm sorry, I know this question has been asked countless times but I was not able to find the difference between the working stuff on here and mine, which is not working.

Also, if there is a better way to achieve what i am trying to do, tell me, I am just learning Java in school right now, so I am far from knowing what I am doing...

So, my Problem, we were supposed to create a game for class, I decided to do Pacman and it's working just fine. The only thing I want to change is being able to save and load levels you created and I thought I was going to do that by storing these in files.

Now, my level consists of squares I called "FELD", that's what it looks like:

import java.io.Serializable;
public class FELD implements Serializable
{
    private static final long serialVersionUID = 1L;
    public int inhalt, inhaltneu; //content, 0=wall, 1=food, 2=empty 3=start
    public int x, y;
    public static int groesse; //size of the square
    public FELD(int a, int b)
    {
        x=a*groesse;
        y=b*groesse;
        PACMAN.zf.zeichneRechteck(x, y, groesse, groesse); //draws a rectangle using a class we have to use in school
        inhalt=0;
    }
}

I left out the other two methods, because they should not be important for this.

Now, to save them I created a class called "LEVELFILE":

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;;
public class LEVELFILE
{
    public FELD[][] lvl;
    public LEVELFILE()
    {

    }

    public boolean speichern(FELD[][] a) {
        lvl=a;

        String name="lvl1.ser";
        try {
            FileOutputStream fos = new FileOutputStream(name);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(lvl);
            oos.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        PACMAN.zf.fuelleRechteck(0, 0, 100, 100, "rot"); //draws a red rectangle to tell me it's done saving, works
        return true;
    }

    public FELD[][] ladenlvl1() {
        String name="lvl1.ser";
        try {
            FileInputStream fis = new FileInputStream(name);
            ObjectInputStream ois = new ObjectInputStream(fis);
            lvl = (FELD[][]) ois.readObject();
            ois.close();
        }

        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        PACMAN.zf.fuelleRechteck(0, 0, 100, 100, "gruen"); //draws a green rectangle to tell me it's done loading, doesn't happen
        return lvl;
    }
}

So, a few things, first off, is it okay to just say oos.writeObject(lvl), when lvl is a two-dimensional array?

Same thing for reading, is that fine?

Also, what is this printStackTrace();? I found that on here multiple times so I just copied it, but what does it actually do?

Since i don't get the green rectangle, the program is supposed to draw, I guess it somehow gets stuck, but I don't know where it would. That's what I was hoping you guys might be able to tell me.

Also, if you know of a better/simpler way of saving my levels to a file, please tell me, as I said, I am far from knowing anything about this.

And, since that is a project we have been doing in school, I did ask my teacher first, but he said, he never used ObjectOutputStream and ObjectInputStream before, so he can't help me, that's why I am here right now.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
Speze
  • 1
  • 1
  • I think what you are looking for is called "Serialization". It allows you to write whole objects to/from a file. Here is a link: https://www.tutorialspoint.com/java/java_serialization.htm – Katianie Jun 01 '18 at 13:01
  • Quoting java documentation on printStackTrace() "Prints this throwable and its backtrace to the standard error stream". Also answered in this thread : [What is the use of printStackTrace() method in Java](https://stackoverflow.com/questions/2560368/what-is-the-use-of-printstacktrace-method-in-java) – JohEker Jun 01 '18 at 13:09

0 Answers0