File fp = new File(filePath);fp.exists() does not yeild proper result on Linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ganeshp
    New Member
    • May 2007
    • 38

    File fp = new File(filePath);fp.exists() does not yeild proper result on Linux

    Below given is the code to check if a file exists.
    This code works on Windows but on Linux it fails even when the file exists.

    Please let me know if you have a solution or a work around for this.
    Has anybody experienced the same problem.

    boolean DoesTheFileExis t(String filePath)
    {
    File fp = new File(filePath);
    if(true == fp.exists())
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    Regards,
    Ganesh
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    It should work. Of course UNIX != Windows. You should take a long look at the path you are passing to this method. By the way, you could have written it more simply as [CODE=Java]boolean DoesTheFileExis t(String filePath) {
    return new File(filePath). exists();
    }[/CODE]

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Could it be a slash versus backslash issue?

      kind regards,

      Jos

      Comment

      • jeffbroodwar
        New Member
        • Oct 2006
        • 118

        #4
        I think Jos is right. there are some problems that occur when your using windows and linux in terms of syntax. guess you better check the documentation for linux.


        P.S. StarCraft II coming soon... En taro adun !!!


        Regards,
        Jeff

        Comment

        • ganeshp
          New Member
          • May 2007
          • 38

          #5
          Originally posted by JosAH
          Could it be a slash versus backslash issue?

          kind regards,

          Jos
          it is not a slash versus backslash issue.

          Has anyone used this kind of code on linux,
          i am really facing problems with this exists() API.
          it is really causing problems.

          I gave the path as /tmp/file.txt on linux and c:\file.txt on windows.
          On windows i get the correct answer but on linux the
          API returns false even when the file exists.

          Also for your information i am using "jdk1.5.0_1 3", i don't think that is an issue or is it?

          Java gurus , please come forward and answer this problem.

          Please ,anybody who has used this API on Linux please come forward and post your thoughts.

          Please can any body shed some light on this issue, i am eagerly waiting for more posts

          regards,
          Ganesh

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by ganeshp
            it is not a slash versus backslash issue.

            Has anyone used this kind of code on linux,
            i am really facing problems with this exists() API.
            it is really causing problems.

            I gave the path as /tmp/file.txt on linux and c:\file.txt on windows.
            On windows i get the correct answer but on linux the
            API returns false even when the file exists.

            Also for your information i am using "jdk1.5.0_1 3", i don't think that is an issue or is it?

            Java gurus , please come forward and answer this problem.

            Please ,anybody who has used this API on Linux please come forward and post your thoughts.

            Please can any body shed some light on this issue, i am eagerly waiting for more posts

            regards,
            Ganesh

            I think it is a Linux issue.
            On which user your are running this Script.
            May be from that user the file can't be accessed.
            Though I am not expert on Unix.


            Debasis Jana

            Comment

            • ganeshp
              New Member
              • May 2007
              • 38

              #7
              Originally posted by dmjpro
              I think it is a Linux issue.
              On which user your are running this Script.
              May be from that user the file can't be accessed.
              Though I am not expert on Unix.


              Debasis Jana
              Thanks Debasis Jana for your interest.

              I am running it as a root user.
              Since the use is root, i guess there should be no previlage related issues.
              Also the permission for the file is rw-r-r. So any user should be able to read this file.

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Originally posted by ganeshp
                Thanks Debasis Jana for your interest.

                I am running it as a root user.
                Since the use is root, i guess there should be no previlage related issues.
                Also the permission for the file is rw-r-r. So any user should be able to read this file.
                The case sensitivity check please.
                May be it is sure shot answer. :)

                Debasis Jana

                Comment

                • ganeshp
                  New Member
                  • May 2007
                  • 38

                  #9
                  Originally posted by dmjpro
                  The case sensitivity check please.
                  May be it is sure shot answer. :)

                  Debasis Jana

                  I tried to print the absolute path.

                  Even thought i enter the path as "/tmp/file.txt"
                  when i print the absolute path, the output is "c:\tmp\file.tx t"

                  i.e. fp.getAbsoluteP ath() prints "c:\tmp\file.tx t" when i enter "/tmp/file.txt".

                  I do not know how this conversion is taking place , because i am not making this conversion.

                  does anybody know whats happening here?

                  Comment

                  • ganeshp
                    New Member
                    • May 2007
                    • 38

                    #10
                    My code is a web application, this was written on windows using eclipse,
                    Ant was used to build the code on windows.
                    Tomcat is the web-server that i am using and it is running on windows.

                    Say for example link for my web page is abc.com
                    Here i ask user to specify absolute path of a file on his system.
                    Now when the user is accessing my web-page form Linux box he enters the file path as
                    /tmp/file.txt which is correct as he is using the Linux machine.

                    I debugged my application just now ,
                    in code the File fp = new File(filePath);
                    the path is still /tmp/file.txt , But when is do fp.getAbsoluteP ath() the string returned is C:\tmp\file.txt
                    This is incorrect right ? because the user is on Linux-box and he entered /tmp/fiile.txt
                    And fp.exists() returns false.

                    I am not understanding this behavior !

                    Comment

                    • dmjpro
                      Top Contributor
                      • Jan 2007
                      • 2476

                      #11
                      Originally posted by ganeshp
                      I tried to print the absolute path.

                      Even thought i enter the path as "/tmp/file.txt"
                      when i print the absolute path, the output is "c:\tmp\file.tx t"

                      i.e. fp.getAbsoluteP ath() prints "c:\tmp\file.tx t" when i enter "/tmp/file.txt".

                      I do not know how this conversion is taking place , because i am not making this conversion.

                      does anybody know whats happening here?

                      I am little bit confusing here.
                      Let me understand.
                      Please send me the code fragment what you tried in Unix and in Windows.
                      And print the output as you are getting.
                      And paste that "getAbsolutePat h()" also.

                      Debasis Jana

                      Comment

                      Working...