Checking for a valid file name

Discussion in 'Java' started by m, Aug 8, 2007.

  1. m

    m Guest

    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.
     
    m, Aug 8, 2007
    #1
    1. Advertising

  2. 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
     
    Flo 'Irian' Schaetz, Aug 8, 2007
    #2
    1. Advertising

  3. m

    Lew Guest

    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.

    --
    Lew
     
    Lew, Aug 8, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex

    checking valid filename

    Alex, Jan 31, 2004, in forum: Java
    Replies:
    8
    Views:
    13,073
    Jon A. Cruz
    Feb 8, 2004
  2. Berlin  Brown
    Replies:
    3
    Views:
    1,247
    Berlin Brown
    Jul 1, 2005
  3. Martin Drautzburg

    wxTreeCtrl checking for valid IDs

    Martin Drautzburg, Dec 27, 2004, in forum: Python
    Replies:
    0
    Views:
    427
    Martin Drautzburg
    Dec 27, 2004
  4. Peter L. Berghold
    Replies:
    6
    Views:
    573
  5. Joe Van Dyk

    Checking for valid data in file

    Joe Van Dyk, Mar 16, 2006, in forum: C++
    Replies:
    5
    Views:
    373
    Victor Bazarov
    Mar 17, 2006
Loading...

Share This Page