You are on page 1of 2

public void myFileDialogAndSave( )

{
String myFileName;
String theText = "This is the text to save.\n";
java.io.FilePermission perm = null;
try
{
myFileName = myFileDialog();
perm = new FilePermission(myFileName, "read,write");
AccessController.checkPermission(perm);
mySave(myFileName, theText);
}
catch(AccessControlException e)
{
}
catch( IOException e )
{
}
}
public String myFileDialog()
{
String path, name;
FileDialog fd;
Frame fdFrame;
fdFrame = new Frame();
fd = new FileDialog(fdFrame, "FileName: ", FileDialog.SAVE);
fd.setModal(true);
fd.setFile("a_name.txt");
fd.setDirectory("");
fd.setVisible(true);
path = fd.getDirectory();
name = fd.getFile();
if( name == null || name.equalsIgnoreCase("null"))
name = "default_name.txt";
fd.setVisible(false);
fdFrame.setVisible(false);
return (path + name);
}
public void mySave(String myFileName, String theText)
throws IOException
{
File myFile;
FileWriter out;
myFile = new File(myFileName);
out = new FileWriter(myFile);
out.write(theText, 0, theText.length());
out.flush();
out.close();
}

You might also like