-1

I am trying to save a game state with no success.

public void saveGame(){
board = GameBoard.this;

try (
  OutputStream file = new FileOutputStream(board);    <---- error in this line
  OutputStream buffer = new BufferedOutputStream(file);
  ObjectOutput output = new ObjectOutputStream(buffer);
){
  output.writeObject(game);
}  
catch(IOException ex){
  fLogger.log(Level.SEVERE, "Cannot save.", ex);
}

}

The error I receive for the line indicated is that it is not a suitable constructor. I'm completely lost. Can someone help me get this working or give me pointers as to where I have gone wrong please. I am not familiar with serializeable.

AW90
  • 53
  • 1
  • 10

5 Answers5

0

You need to provide the FileOutputStream with a File to work with:

OutputStream file = new FileOutputStream(new File("pathToMyFile.txt"));
Loki
  • 4,065
  • 4
  • 29
  • 51
0

From the documentation you can read that there are 2 one parameter constructors:

FileOutputStream(File file)
FileOutputStream(FileDescriptor fdObj)
FileOutputStream(String name)

so since your compiler complains and I have no idea what GameBoard is (which you feed your constructor with) check if your GameBoard is by any chance of these. If not then you should do something about it.

Eypros
  • 5,370
  • 6
  • 42
  • 75
0

According to the documentation fo FileOutputStream, there are a few constructors for the FileOutputStream class:

  1. Using a File object
  2. Using a File object and a boolean
  3. Using a FileDescriptor object
  4. Using a String object
  5. Using a String object and a boolean value

Since you're using a constructor which you supply with one parameter, you're using either option 1, 3 or 5. The board variable has type GameBoard, which is not File, FileDescriptor or String, so the compiler tells you there is no constructor that matches the type of variable board.

The easiest is to give the FileOutputStream constructor a filename to write to. Other options are a File object or a FileDescriptor.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
0

As it says in the javadocs, FileOutputStream expects a String or File The error message is trying to tell you, you are trying to pass an object to the constructor it doesn't support.

I suggest you give it to filename you want it to write to.

Other suggestions

  • You don't need to buffer an ObjectOutputStream as it is already buffered.
  • When you have a buffered stream you should always close (or flush it) otherwise you will have unwritten data, possibly the file will be empty.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

See http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html for the constructors of FileOutputStream. The first one will do perfectly fine:

OutputStream fos = new FileOutputStream(new File("/path/object.dat"));

Halmackenreuter
  • 681
  • 5
  • 9