Results 1 to 12 of 12

Thread: Args[0]

  1. #1
    hakan123 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default Args[0]

    Hello All!
    I am new to Java. I run my java binary from command prompt, where i can run it with a file name, or with the full path of the file(it contains filename in the end). now i want to check whether the argument passed is just a filename or it is the fullpath.if it is the full path(and file name in the end), i want to read the filename from it,
    please help me.
    thanks

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    13

    Default

    So, if it contains one or more / or \ you've got a full path with maybe a filemane at the end, but you'd still have to check whether 3:/fff/barf/foo.bar/barf is a valid path to a file or just garbage.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    hakan123 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default Thanks!

    Thenks Phein!
    but still i did not get how i would be able to do it. and let us suppose that in my case the argument is always a valid path(if it is not a filename).

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    13

    Default

    You could create a File object from the parameter and use the exists() method to find out whether the file is in your file system. If it is there you've got a valid path to a file.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    hakan123 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default

    Thanks!
    thanks phein for your reply. but that is what i want. I want to retreive the Filename from the path(if the input argument in a path instid of file)

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    13

    Default

    Then split the argument, the last token is your file name.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    hakan123 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default

    thanks Phein!
    but i wonder if i split my argument, then the last token will be my filename:this will be valid for the case when the argument passed will be a path. my question: if i use split string, will it work in the case when arguemnt is filename???

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    13

    Default

    Yes it will, why don't you just try it?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    13

    Default

    Oh, and another one both these are paths:
    c:/foo/doc.doc
    c:\foo\doc.doc
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  10. #10
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    11

    Default

    args[0].replace("\\","/") will solve that phhein :)

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    13

    Default

    I know that, but I wanted to give hakan123 a chance to do some research and to learn something new instead of spoon feeding him.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  12. #12
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    11

Similar Threads

  1. Why can't we write main without String args[]
    By shailender in forum New To Java
    Replies: 6
    Last Post: 06-17-2013, 09:28 AM
  2. What does String args[] stand for?
    By Addez in forum New To Java
    Replies: 7
    Last Post: 08-19-2009, 10:24 AM
  3. How to add Args at runtime in JCreator
    By JordashTalon in forum JCreator
    Replies: 0
    Last Post: 01-28-2009, 10:12 PM
  4. problem in args
    By MS_Dark in forum New To Java
    Replies: 13
    Last Post: 08-27-2008, 01:43 AM
  5. command line args
    By MarkWilson in forum NetBeans
    Replies: 3
    Last Post: 08-04-2008, 03:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •