We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Saving file with specified file extention
Page Index Toggle Pages: 1
Saving file with specified file extention (Read 823 times)
Saving file with specified file extention
May 25th, 2010, 6:06am
 
Hi,
i'm asking for help with saving files.
A have got a following code:

It saves file only if i set the *.xxx extention

Code:

FileDialog fd = new FileDialog(this.frame);
fd.setMode(FileDialog.SAVE);
fd.setLocation(50, 50);
fd.show();
String dir = fd.getDirectory();
String file = fd.getFile();
if( dir != null && file != null && file.indexOf(".xxx") != -1) {
PrintWriter out;
out = createWriter(dir+file);

out.println("text");

out.flush();
out.close();
}


I would like to run file dialog than chose a directory and enter only file name.
i want file dialog to save my file with presed extention 'xxx'.
How to specify file extention to be add to any saved files without writing it in after file name in filedialog ???????????

HELP
Re: Saving file with specified file extention
Reply #1 - May 25th, 2010, 7:04am
 
I don't understand your question. What is the problem of adding manually the file extension if it is not there?

BTW, don't do file.indexOf(".xxx") != -1, you rather want: file.endsWith(".xxx")

So, it should be something like:
Code:
String dir = fd.getDirectory();
String file = fd.getFile();
if (dir != null && file != null) {
String path = dir + file;
if (!file.toLowerCase().endsWith(".xxx")) {
path += ".xxx";
}
PrintWriter out;
out = createWriter(path);

out.println("text");

out.close(); // Close does the flush, actually
}
Re: Saving file with specified file extention
Reply #2 - May 25th, 2010, 9:01am
 
Thak You,
This is what I looked for Smiley

btw Sory, my english isn't perfect :p
Page Index Toggle Pages: 1