Reputation: 1
public void savemap (File file)
{
if (file == null) {
if (chooser == null) chooser = createChooser();
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
int r = chooser.showDialog(this, null);
if (r != JFileChooser.APPROVE_OPTION) return;
file = chooser.getSelectedFile();
} /* get the selected file */
try { /* save the current TSP */
FileWriter writer = new FileWriter(file);
writer.write();
writer.close(); }
catch (IOException e) {
String msg = e.getMessage();
this.stat.setText(msg); System.err.println(msg);
JOptionPane.showMessageDialog(this, msg,
"Error", JOptionPane.ERROR_MESSAGE);
} /* set the status text */
this.curr = file; /* note the new file name */
}
Edited
try {
int rows = 50;
int columns = 50;
Cell [][] cellArray = new Cell[columns][rows];
FileOutputStream fout = new FileOutputStream(FILE_PATH);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(cellArray);
}
//This is what i changed the code. Is it correct? However i have an error at (FILE_PATH) FILE_PATH cannot be resolved to a variable
Upvotes: 0
Views: 153
Reputation: 32670
To elaborate on my comment: When you call writer.write( )
, it expects you to give it a parameter that tells it what to write. Without that parameter, your code should not be compiling.
String s = "foobar"
writer.write(s)
or just:
writer.write("foobar")
will write a String
value to file.
A second problem you have is that FileWriter
(or more accurately, Writer
, which it derives from) is only capable of writing String
s, char
s and int
s by itself. If you want to write arbitrary Objects to file, you'll need to use an ObjectOutputStream
:
RandomObject rando = new RandomObject();
FileOutputStream fout = new FileOutputStream(FILE_PATH);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(rando);
You might find this tutorial useful.
Upvotes: 2