Why do we have to add forward slash in the file directory to differentiate between directory and files?












1















Inside the For loop:



Why do I have to add a forward slash after the directory name?
For example:



for(int i = 0; i<s.length; i++){
File f = new File(dirname + "/" + s[i] );
// Why to add "/" after dirname(i.e directory name)
if(f.isDirectory()){
System.out.println(s[i] + " is Directory" );
}else{
System.out.println(s[i] + " is File");
}
}


If I remove backslash "/" after dirname:



i.e In First Code



File f = new File(dirname + "/" + s[i] ); 


When I remove "/".



In the Second code:



File f = new File(dirname + s[i] ); 


It won't differentiate between Directory and File. All the Files inside will be considered to be the file.
After I add a backslash, it will be okay. And it will differentiate between Directory and File. Why is that?
Why do I have to add "/". The program is meant to look inside the file without adding "/".










share|improve this question

























  • We can't say until we know what variables such as s and dirname are.

    – Kilves
    Jan 3 at 11:38
















1















Inside the For loop:



Why do I have to add a forward slash after the directory name?
For example:



for(int i = 0; i<s.length; i++){
File f = new File(dirname + "/" + s[i] );
// Why to add "/" after dirname(i.e directory name)
if(f.isDirectory()){
System.out.println(s[i] + " is Directory" );
}else{
System.out.println(s[i] + " is File");
}
}


If I remove backslash "/" after dirname:



i.e In First Code



File f = new File(dirname + "/" + s[i] ); 


When I remove "/".



In the Second code:



File f = new File(dirname + s[i] ); 


It won't differentiate between Directory and File. All the Files inside will be considered to be the file.
After I add a backslash, it will be okay. And it will differentiate between Directory and File. Why is that?
Why do I have to add "/". The program is meant to look inside the file without adding "/".










share|improve this question

























  • We can't say until we know what variables such as s and dirname are.

    – Kilves
    Jan 3 at 11:38














1












1








1








Inside the For loop:



Why do I have to add a forward slash after the directory name?
For example:



for(int i = 0; i<s.length; i++){
File f = new File(dirname + "/" + s[i] );
// Why to add "/" after dirname(i.e directory name)
if(f.isDirectory()){
System.out.println(s[i] + " is Directory" );
}else{
System.out.println(s[i] + " is File");
}
}


If I remove backslash "/" after dirname:



i.e In First Code



File f = new File(dirname + "/" + s[i] ); 


When I remove "/".



In the Second code:



File f = new File(dirname + s[i] ); 


It won't differentiate between Directory and File. All the Files inside will be considered to be the file.
After I add a backslash, it will be okay. And it will differentiate between Directory and File. Why is that?
Why do I have to add "/". The program is meant to look inside the file without adding "/".










share|improve this question
















Inside the For loop:



Why do I have to add a forward slash after the directory name?
For example:



for(int i = 0; i<s.length; i++){
File f = new File(dirname + "/" + s[i] );
// Why to add "/" after dirname(i.e directory name)
if(f.isDirectory()){
System.out.println(s[i] + " is Directory" );
}else{
System.out.println(s[i] + " is File");
}
}


If I remove backslash "/" after dirname:



i.e In First Code



File f = new File(dirname + "/" + s[i] ); 


When I remove "/".



In the Second code:



File f = new File(dirname + s[i] ); 


It won't differentiate between Directory and File. All the Files inside will be considered to be the file.
After I add a backslash, it will be okay. And it will differentiate between Directory and File. Why is that?
Why do I have to add "/". The program is meant to look inside the file without adding "/".







java eclipse file directory backslash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 11:47









Andy Turner

84k983143




84k983143










asked Jan 3 at 11:35









sophinsophin

314




314













  • We can't say until we know what variables such as s and dirname are.

    – Kilves
    Jan 3 at 11:38



















  • We can't say until we know what variables such as s and dirname are.

    – Kilves
    Jan 3 at 11:38

















We can't say until we know what variables such as s and dirname are.

– Kilves
Jan 3 at 11:38





We can't say until we know what variables such as s and dirname are.

– Kilves
Jan 3 at 11:38












2 Answers
2






active

oldest

votes


















3














Including and omitting the / mean that the file points to a different path. For example, "foo/bar" and "foobar" are different paths, which would point to different objects in the file system:



Parent directory
+-- foobar "foobar"
+-- foo
+-- bar "foo/bar"


Not being a directory is not the same thing as being a file. So, most likely (we don't know what's in your file system), it's not a directory because it doesn't exist.



You should check if (!f.exists()) (or similar) first:



if (!f.exists()) System.out.println("Doesn't exist");
else if (f.isDirectory()) ... etc


Also, note that you shouldn't add a / anyway - use the two-arg constructor:



File f = new File(dirname, s[i] ); 





share|improve this answer
























  • Thanks for the answer. It really helped me!!

    – sophin
    Jan 6 at 4:46



















0














Slash symbol is a file separator. This is used to separate the directories from the files.



If you are to build your string, the file name will be:



Pictures/my-dog.png


Here, you will see that there is a directory named "Picture" and a filename "my-dog.png".



If you were to remove the slash, it will look like a long filename:



Picturesmy-dog.png





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021503%2fwhy-do-we-have-to-add-forward-slash-in-the-file-directory-to-differentiate-betwe%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Including and omitting the / mean that the file points to a different path. For example, "foo/bar" and "foobar" are different paths, which would point to different objects in the file system:



    Parent directory
    +-- foobar "foobar"
    +-- foo
    +-- bar "foo/bar"


    Not being a directory is not the same thing as being a file. So, most likely (we don't know what's in your file system), it's not a directory because it doesn't exist.



    You should check if (!f.exists()) (or similar) first:



    if (!f.exists()) System.out.println("Doesn't exist");
    else if (f.isDirectory()) ... etc


    Also, note that you shouldn't add a / anyway - use the two-arg constructor:



    File f = new File(dirname, s[i] ); 





    share|improve this answer
























    • Thanks for the answer. It really helped me!!

      – sophin
      Jan 6 at 4:46
















    3














    Including and omitting the / mean that the file points to a different path. For example, "foo/bar" and "foobar" are different paths, which would point to different objects in the file system:



    Parent directory
    +-- foobar "foobar"
    +-- foo
    +-- bar "foo/bar"


    Not being a directory is not the same thing as being a file. So, most likely (we don't know what's in your file system), it's not a directory because it doesn't exist.



    You should check if (!f.exists()) (or similar) first:



    if (!f.exists()) System.out.println("Doesn't exist");
    else if (f.isDirectory()) ... etc


    Also, note that you shouldn't add a / anyway - use the two-arg constructor:



    File f = new File(dirname, s[i] ); 





    share|improve this answer
























    • Thanks for the answer. It really helped me!!

      – sophin
      Jan 6 at 4:46














    3












    3








    3







    Including and omitting the / mean that the file points to a different path. For example, "foo/bar" and "foobar" are different paths, which would point to different objects in the file system:



    Parent directory
    +-- foobar "foobar"
    +-- foo
    +-- bar "foo/bar"


    Not being a directory is not the same thing as being a file. So, most likely (we don't know what's in your file system), it's not a directory because it doesn't exist.



    You should check if (!f.exists()) (or similar) first:



    if (!f.exists()) System.out.println("Doesn't exist");
    else if (f.isDirectory()) ... etc


    Also, note that you shouldn't add a / anyway - use the two-arg constructor:



    File f = new File(dirname, s[i] ); 





    share|improve this answer













    Including and omitting the / mean that the file points to a different path. For example, "foo/bar" and "foobar" are different paths, which would point to different objects in the file system:



    Parent directory
    +-- foobar "foobar"
    +-- foo
    +-- bar "foo/bar"


    Not being a directory is not the same thing as being a file. So, most likely (we don't know what's in your file system), it's not a directory because it doesn't exist.



    You should check if (!f.exists()) (or similar) first:



    if (!f.exists()) System.out.println("Doesn't exist");
    else if (f.isDirectory()) ... etc


    Also, note that you shouldn't add a / anyway - use the two-arg constructor:



    File f = new File(dirname, s[i] ); 






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 3 at 11:44









    Andy TurnerAndy Turner

    84k983143




    84k983143













    • Thanks for the answer. It really helped me!!

      – sophin
      Jan 6 at 4:46



















    • Thanks for the answer. It really helped me!!

      – sophin
      Jan 6 at 4:46

















    Thanks for the answer. It really helped me!!

    – sophin
    Jan 6 at 4:46





    Thanks for the answer. It really helped me!!

    – sophin
    Jan 6 at 4:46













    0














    Slash symbol is a file separator. This is used to separate the directories from the files.



    If you are to build your string, the file name will be:



    Pictures/my-dog.png


    Here, you will see that there is a directory named "Picture" and a filename "my-dog.png".



    If you were to remove the slash, it will look like a long filename:



    Picturesmy-dog.png





    share|improve this answer




























      0














      Slash symbol is a file separator. This is used to separate the directories from the files.



      If you are to build your string, the file name will be:



      Pictures/my-dog.png


      Here, you will see that there is a directory named "Picture" and a filename "my-dog.png".



      If you were to remove the slash, it will look like a long filename:



      Picturesmy-dog.png





      share|improve this answer


























        0












        0








        0







        Slash symbol is a file separator. This is used to separate the directories from the files.



        If you are to build your string, the file name will be:



        Pictures/my-dog.png


        Here, you will see that there is a directory named "Picture" and a filename "my-dog.png".



        If you were to remove the slash, it will look like a long filename:



        Picturesmy-dog.png





        share|improve this answer













        Slash symbol is a file separator. This is used to separate the directories from the files.



        If you are to build your string, the file name will be:



        Pictures/my-dog.png


        Here, you will see that there is a directory named "Picture" and a filename "my-dog.png".



        If you were to remove the slash, it will look like a long filename:



        Picturesmy-dog.png






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 12:31









        KaNa0011KaNa0011

        615615




        615615






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021503%2fwhy-do-we-have-to-add-forward-slash-in-the-file-directory-to-differentiate-betwe%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas