Checking for a valid file name

M

m

Hi All

I am writing a swing application.

users should enter filename (should be .txt) as input in a textbox,
they should enter absolute path.

I need to check if the file name entered is a valid , before creating
the file.

if the user enters c:\mydir\sample.txt.
- I need to make sure that c:\mydirs is a vaild directory and I have
write permissions to write in the directory
-That user entered .a valid text file (ending with .txt in the file
name)
- handle conditions like empty string in file name
- make sure they entered full path instead of just file name.
-make sure they used proper file separator
etc

Is there a better way to do this..

When I am coding for this..
I am writing it like a series of if - then -else statements


if( StringUtils.isEmpty(destnFile) == true)
{
JOptionPane.showMessageDialog(this, "Please enter a valid
file name");
return;
}
if( destnFile.endsWith(".txt") == false)
{
JOptionPane.showMessageDialog(this, "Converted file should
be an text file (.txt)");
return;
}

String scriptDir =
destnFile.substring(0,destnFile.lastIndexOf(File.pathSeparator));
File scriptDirLoc = new File(scriptDir);
if( scriptDirLoc.isDirectory() == false)
{
JOptionPane.showMessageDialog(this, scriptDir+": is not a
valid directory");
return;
}


Please let me know.
 
F

Flo 'Irian' Schaetz

And thus spoke m...
I need to check if the file name entered is a valid , before creating
the file.

if the user enters c:\mydir\sample.txt.
- I need to make sure that c:\mydirs is a vaild directory and I have
write permissions to write in the directory
-That user entered .a valid text file (ending with .txt in the file
name)
- handle conditions like empty string in file name
- make sure they entered full path instead of just file name.
-make sure they used proper file separator
etc

JFileChooser with FileFilters.

Flo
 
L

Lew

And thus spoke m...
That's two independent conditions expressed in one bullet point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top