208.254.225.8 writes:
I am trying to make a file write to disk and this code compiles fine. The save dialog box appeares and you can use it normally, but when you press save, nothing happens. Does anybody know the right way(as opposed to my way) of doing this? Thanks.
---------------
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.util.zip.*;
public class HelloWorld extends Applet {
public void init() {
resize(130, 60);
setBackground(Color.white);
}
char currentChar = ' ';
String fosString = new String("typed" + String.valueOf(currentChar));
public boolean keyDown(Event evt, int x) {
currentChar = (char)x;
repaint();
return true;
}
public void paint(Graphics g) {
g.drawString("You have just typed " + String.valueOf(currentChar), 1, 50);
}
public Button saver = new Button("Save currentChar"); {
add(saver);
}
public Frame Frame = new Frame();
public void save() {
FileDialog a = new FileDialog(Frame, "Save Char", FileDialog.SAVE);
a.show();
String filename = a.getFile();
if (filename != null) {
try {
FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject("fosString");
out.flush();
out.close();
}
catch (IOException e) { System.out.println(e); }
}
}
public boolean action(Event e, Object arg) {
if (e.target == saver) {
save();
return true;
}
else return false;
}
}
---------------
R. O'Hare
24.1.16.214 writes:
in your code, you have this:
out.writeObject("fosString");
I am suspicious of the double quotes. in double quotes you get the literal string "fosString"
in order to write out the object refered to by the fosString handle, you do not want to use the double quotes.
-calyxa
208.255.86.226 writes:
Dang, I tried it and it gave me an error about that object not existing so it does recognize the string as a reference to an object. On comp.lang.java.help someone wrote to me on the same topic. It all sounds like greek to a newbie like myself, but it looks kinda like it makes sense. Any ideas on this? Thanks.
----------
"When you've shown the dialog, you need to read the filename and directory selected, and if they are not null (as they
would be if the user hit "Cancel"), then you have to write the data
to that file yourself - just like you have to read it yourself after an open dialog."
----------
R. O'Hare
208.255.86.226 writes:
Never mind, I was stupidly trying to execute it as an applet rather than an application. After I made it an application it worked just fine. Thanks anyway.
R. O'Hare