![]() |
File extensions
Hi, i'm trying to add a file extension to a file when the user has typed in the filename without an extension (in a JFileChooser). I've managed to recognise that the file is missing an extension.
:
String extension = null;And from here i can work out the required extension from the filefilters but i don't know how to rename the file before i save it. Can anyone help? |
Try the renameTo method of the File class.
|
Hi thanks. But my understanding of renameTo is that it renames a 'physical' file on disk. However my file object has not necessarily been stored to disk. I.e. it's the first time the filename is being used. Here is my code (some of it). The way i need it to work is, if you try to save the file with no extension then the filename is automatically appended to '.txt'. Thanks.
:
import java.awt.*; |
I've solved it now.
I just appended the parameter in the FileWriter :
FileWriter fileWriter = new FileWriter(fileName + ext); |
But what if filename is "something.txt"?
:
if (!filename.substr(filename.length() - 4).equals(ext)) |
In the actual application the setExtention method returns a null value to a string variable if the file has an extension. Then depending on that variable the file is save with or without an appended extension.
|
| All times are GMT -5. The time now is 2:20 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC