Get full path file name string from user, use to create File object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Get full path file name string from user, use to create File object

  1. #1
    Join Date
    Nov 2003
    Posts
    2

    Get full path file name string from user, use to create File object

    I have a piece of code below that uses the console to prompt the user for a complete path to an input file. No matter what I do with regex I can't get the full path to be assigned to a string such that File can use it to check for the file's existence. Further below is the method that attempts to do the actual check. It always returns false no matter what I do.

    Note that I'm expecting a windows path as the input from the user "c:\some\path\file", NOT "c:\\some\\path\\file".


    BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
    String strInput;

    System.out.print("\nEnter full path to database: ");
    strInput = bufr.readLine();

    if(setSource(strInput) == false) //source database invalid?
    {
    System.out.println("\nInvalid database\n");
    return; // exit program
    }



    private boolean setSource(String pStr)
    {
    String strTemp = pStr.replaceAll("\\\\", "\\\\\\\\");
    File filTemp = new File(strTemp);

    if(filTemp.exists())
    {
    return true;
    }
    else
    {
    return false;
    }
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    you dont have to convert strings with slash characters at runtime.. it is the compiler that looks at \ and expects them to be escape codes. If you want a slash in a string at COMPILE TIME:

    String s = "slash\\in string"

    if you want a slash in a regex, the compiler interprets double slash to single, but then the regex compiler interprets slash to special too, so for a single slash as a slash character in a regex (and not an escape character) you must use 4 slashes:

    Pattern.compile("regex with literal slash here:\\\\")
    matches string: regex with literal slash here:\

    but for user entered strings ALWAYS it is the case that a single slash is a single slash. users cant enter escape codes without you searching the string, finding \, parsing the entry after to see if it makes an escape code \n \b \h \r \uXXXX and so on.. hope this makes sense



    have a play with my altered version of your program and you will see:

    Code:
    import java.io.*;
    public class test123{
    public static void main(String[] f)throws Exception{
    
    
    BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
    String strInput;
    
    System.out.print("\nEnter full path to database: ");
    strInput = bufr.readLine();
    
    if(new File(strInput).exists()) //source database invalid?
    System.out.println("\nThe file exists\n");
    else
    System.out.println("\nThe file does not exists\n");
    }}

    C:\javawork\trash>java test123

    Enter full path to database: c:\autoexec.bat

    The file exists


    C:\javawork\trash>java test123

    Enter full path to database: c:\dklhsfhgsdfg

    The file does not exists


    C:\javawork\trash>
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Nov 2003
    Posts
    2
    YES. Thanks, all works well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center