Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Checking for a valid file name

Thread view: 
m - 08 Aug 2007 20:45 GMT
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.
Flo 'Irian' Schaetz - 08 Aug 2007 22:33 GMT
And thus spoke m...

> I need to check if the file name entered is a valid , before creating
> the file.
[quoted text clipped - 8 lines]
> -make sure they used proper file separator
> etc

JFileChooser with FileFilters.

Flo
Lew - 08 Aug 2007 23:02 GMT
And thus spoke m...
>> -That user entered .a valid text file (ending with .txt in the file
>> name)

That's two independent conditions expressed in one bullet point.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.