How to read file in resources folder












-3















Java and IntelliJ newbie here.Apologies if this has been answered already but I cannot seem to find the answer.



My project structure is that shown below:



project



I am trying to work out how to read the contents of the highllighted storedQueries.txt file as a single string. This must be simple and I've tried a number of methods involving getClass and so on, but nothing has worked thus far. Would appreciate any guidance, thanks.










share|improve this question





























    -3















    Java and IntelliJ newbie here.Apologies if this has been answered already but I cannot seem to find the answer.



    My project structure is that shown below:



    project



    I am trying to work out how to read the contents of the highllighted storedQueries.txt file as a single string. This must be simple and I've tried a number of methods involving getClass and so on, but nothing has worked thus far. Would appreciate any guidance, thanks.










    share|improve this question



























      -3












      -3








      -3








      Java and IntelliJ newbie here.Apologies if this has been answered already but I cannot seem to find the answer.



      My project structure is that shown below:



      project



      I am trying to work out how to read the contents of the highllighted storedQueries.txt file as a single string. This must be simple and I've tried a number of methods involving getClass and so on, but nothing has worked thus far. Would appreciate any guidance, thanks.










      share|improve this question
















      Java and IntelliJ newbie here.Apologies if this has been answered already but I cannot seem to find the answer.



      My project structure is that shown below:



      project



      I am trying to work out how to read the contents of the highllighted storedQueries.txt file as a single string. This must be simple and I've tried a number of methods involving getClass and so on, but nothing has worked thus far. Would appreciate any guidance, thanks.







      java embedded-resource






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 18:45









      Andrew Thompson

      153k28162345




      153k28162345










      asked Jan 1 at 15:37









      user3202399user3202399

      59110




      59110
























          3 Answers
          3






          active

          oldest

          votes


















          1














          You can read the file using current class loader but before that, you need to straighten up the structure a bit. When you use ClassLoader to read file, by default it looks into the same package it belongs to.



          Step 1: Create new package com.pe.queries

          Step 2: Create the class Package-Info.java in above package

          Step 3: Create new nested directories under resources as resources/com/pe/queries and move the file to this directory.

          Step 4: Finally you should be able to read the file as
          Package-Info.class.getResource("storedQueries.txt");






          share|improve this answer































            0














            getClass().getResource("/resources/etc.txt")


            Put the resource folder inside target






            share|improve this answer































              0














              There are two ways how you can get your resource:




              1. By absolute path, but itsn't good way, cause absolute paths are unreliable, but it is good for 'smoke'-testing

              2. By relative path, and there are two ways again.


              In both ways you can use getClass().getResource(path) construction.



              Relative paths:




              1. If your resource is in 'root' of resource dir your path should starts with /:

                "/etc.txt"

                Where file etc.txt stored in resources directory.


              For your example in picture your path would be: /queries/storedQueries.txt




              1. If your resources is in resource directory, but in flatten directories, same as packages for class which you invoke .getResource(path), like:


              resources/com/pe/queries/etc.txt



              Your path should not include started '/':
              getClass().getResource("com/pe/queries/etc.txt")



              This is because when your build system(your maven) will build project it put your resource in 'root' of jar if you store it in root of resources directory, or, if you store like resources/com/pe/queries/etc.txt, creates inside jar com/pe/queries/ and put it inside.



              Sorry for my English, hope i help you to understand how it works.



              P.S. I wrote this article, it's on Russian, but you can translate it by Google-transate(i tried and it's understandable), there is more examples and explanations inside.



              Article about resources on Russian



              Good luck!






              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%2f53996730%2fhow-to-read-file-in-resources-folder%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                You can read the file using current class loader but before that, you need to straighten up the structure a bit. When you use ClassLoader to read file, by default it looks into the same package it belongs to.



                Step 1: Create new package com.pe.queries

                Step 2: Create the class Package-Info.java in above package

                Step 3: Create new nested directories under resources as resources/com/pe/queries and move the file to this directory.

                Step 4: Finally you should be able to read the file as
                Package-Info.class.getResource("storedQueries.txt");






                share|improve this answer




























                  1














                  You can read the file using current class loader but before that, you need to straighten up the structure a bit. When you use ClassLoader to read file, by default it looks into the same package it belongs to.



                  Step 1: Create new package com.pe.queries

                  Step 2: Create the class Package-Info.java in above package

                  Step 3: Create new nested directories under resources as resources/com/pe/queries and move the file to this directory.

                  Step 4: Finally you should be able to read the file as
                  Package-Info.class.getResource("storedQueries.txt");






                  share|improve this answer


























                    1












                    1








                    1







                    You can read the file using current class loader but before that, you need to straighten up the structure a bit. When you use ClassLoader to read file, by default it looks into the same package it belongs to.



                    Step 1: Create new package com.pe.queries

                    Step 2: Create the class Package-Info.java in above package

                    Step 3: Create new nested directories under resources as resources/com/pe/queries and move the file to this directory.

                    Step 4: Finally you should be able to read the file as
                    Package-Info.class.getResource("storedQueries.txt");






                    share|improve this answer













                    You can read the file using current class loader but before that, you need to straighten up the structure a bit. When you use ClassLoader to read file, by default it looks into the same package it belongs to.



                    Step 1: Create new package com.pe.queries

                    Step 2: Create the class Package-Info.java in above package

                    Step 3: Create new nested directories under resources as resources/com/pe/queries and move the file to this directory.

                    Step 4: Finally you should be able to read the file as
                    Package-Info.class.getResource("storedQueries.txt");







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 1 at 15:51









                    Yogesh BadkeYogesh Badke

                    1,94611116




                    1,94611116

























                        0














                        getClass().getResource("/resources/etc.txt")


                        Put the resource folder inside target






                        share|improve this answer




























                          0














                          getClass().getResource("/resources/etc.txt")


                          Put the resource folder inside target






                          share|improve this answer


























                            0












                            0








                            0







                            getClass().getResource("/resources/etc.txt")


                            Put the resource folder inside target






                            share|improve this answer













                            getClass().getResource("/resources/etc.txt")


                            Put the resource folder inside target







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 1 at 16:18









                            J. AdamJ. Adam

                            1027




                            1027























                                0














                                There are two ways how you can get your resource:




                                1. By absolute path, but itsn't good way, cause absolute paths are unreliable, but it is good for 'smoke'-testing

                                2. By relative path, and there are two ways again.


                                In both ways you can use getClass().getResource(path) construction.



                                Relative paths:




                                1. If your resource is in 'root' of resource dir your path should starts with /:

                                  "/etc.txt"

                                  Where file etc.txt stored in resources directory.


                                For your example in picture your path would be: /queries/storedQueries.txt




                                1. If your resources is in resource directory, but in flatten directories, same as packages for class which you invoke .getResource(path), like:


                                resources/com/pe/queries/etc.txt



                                Your path should not include started '/':
                                getClass().getResource("com/pe/queries/etc.txt")



                                This is because when your build system(your maven) will build project it put your resource in 'root' of jar if you store it in root of resources directory, or, if you store like resources/com/pe/queries/etc.txt, creates inside jar com/pe/queries/ and put it inside.



                                Sorry for my English, hope i help you to understand how it works.



                                P.S. I wrote this article, it's on Russian, but you can translate it by Google-transate(i tried and it's understandable), there is more examples and explanations inside.



                                Article about resources on Russian



                                Good luck!






                                share|improve this answer






























                                  0














                                  There are two ways how you can get your resource:




                                  1. By absolute path, but itsn't good way, cause absolute paths are unreliable, but it is good for 'smoke'-testing

                                  2. By relative path, and there are two ways again.


                                  In both ways you can use getClass().getResource(path) construction.



                                  Relative paths:




                                  1. If your resource is in 'root' of resource dir your path should starts with /:

                                    "/etc.txt"

                                    Where file etc.txt stored in resources directory.


                                  For your example in picture your path would be: /queries/storedQueries.txt




                                  1. If your resources is in resource directory, but in flatten directories, same as packages for class which you invoke .getResource(path), like:


                                  resources/com/pe/queries/etc.txt



                                  Your path should not include started '/':
                                  getClass().getResource("com/pe/queries/etc.txt")



                                  This is because when your build system(your maven) will build project it put your resource in 'root' of jar if you store it in root of resources directory, or, if you store like resources/com/pe/queries/etc.txt, creates inside jar com/pe/queries/ and put it inside.



                                  Sorry for my English, hope i help you to understand how it works.



                                  P.S. I wrote this article, it's on Russian, but you can translate it by Google-transate(i tried and it's understandable), there is more examples and explanations inside.



                                  Article about resources on Russian



                                  Good luck!






                                  share|improve this answer




























                                    0












                                    0








                                    0







                                    There are two ways how you can get your resource:




                                    1. By absolute path, but itsn't good way, cause absolute paths are unreliable, but it is good for 'smoke'-testing

                                    2. By relative path, and there are two ways again.


                                    In both ways you can use getClass().getResource(path) construction.



                                    Relative paths:




                                    1. If your resource is in 'root' of resource dir your path should starts with /:

                                      "/etc.txt"

                                      Where file etc.txt stored in resources directory.


                                    For your example in picture your path would be: /queries/storedQueries.txt




                                    1. If your resources is in resource directory, but in flatten directories, same as packages for class which you invoke .getResource(path), like:


                                    resources/com/pe/queries/etc.txt



                                    Your path should not include started '/':
                                    getClass().getResource("com/pe/queries/etc.txt")



                                    This is because when your build system(your maven) will build project it put your resource in 'root' of jar if you store it in root of resources directory, or, if you store like resources/com/pe/queries/etc.txt, creates inside jar com/pe/queries/ and put it inside.



                                    Sorry for my English, hope i help you to understand how it works.



                                    P.S. I wrote this article, it's on Russian, but you can translate it by Google-transate(i tried and it's understandable), there is more examples and explanations inside.



                                    Article about resources on Russian



                                    Good luck!






                                    share|improve this answer















                                    There are two ways how you can get your resource:




                                    1. By absolute path, but itsn't good way, cause absolute paths are unreliable, but it is good for 'smoke'-testing

                                    2. By relative path, and there are two ways again.


                                    In both ways you can use getClass().getResource(path) construction.



                                    Relative paths:




                                    1. If your resource is in 'root' of resource dir your path should starts with /:

                                      "/etc.txt"

                                      Where file etc.txt stored in resources directory.


                                    For your example in picture your path would be: /queries/storedQueries.txt




                                    1. If your resources is in resource directory, but in flatten directories, same as packages for class which you invoke .getResource(path), like:


                                    resources/com/pe/queries/etc.txt



                                    Your path should not include started '/':
                                    getClass().getResource("com/pe/queries/etc.txt")



                                    This is because when your build system(your maven) will build project it put your resource in 'root' of jar if you store it in root of resources directory, or, if you store like resources/com/pe/queries/etc.txt, creates inside jar com/pe/queries/ and put it inside.



                                    Sorry for my English, hope i help you to understand how it works.



                                    P.S. I wrote this article, it's on Russian, but you can translate it by Google-transate(i tried and it's understandable), there is more examples and explanations inside.



                                    Article about resources on Russian



                                    Good luck!







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jan 1 at 20:52

























                                    answered Jan 1 at 17:08









                                    aarexeraarexer

                                    327211




                                    327211






























                                        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%2f53996730%2fhow-to-read-file-in-resources-folder%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

                                        Mossoró

                                        Error while reading .h5 file using the rhdf5 package in R

                                        Pushsharp Apns notification error: 'InvalidToken'