How to read file in resources folder
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:

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
add a comment |
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:

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
add a comment |
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:

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
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:

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
java embedded-resource
edited Jan 1 at 18:45
Andrew Thompson
153k28162345
153k28162345
asked Jan 1 at 15:37
user3202399user3202399
59110
59110
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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");
add a comment |
getClass().getResource("/resources/etc.txt")
Put the resource folder inside target
add a comment |
There are two ways how you can get your resource:
- By absolute path, but itsn't good way, cause absolute paths are unreliable, but it is good for 'smoke'-testing
- By relative path, and there are two ways again.
In both ways you can use getClass().getResource(path) construction.
Relative paths:
- 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
- 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!
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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");
add a comment |
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");
add a comment |
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");
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");
answered Jan 1 at 15:51
Yogesh BadkeYogesh Badke
1,94611116
1,94611116
add a comment |
add a comment |
getClass().getResource("/resources/etc.txt")
Put the resource folder inside target
add a comment |
getClass().getResource("/resources/etc.txt")
Put the resource folder inside target
add a comment |
getClass().getResource("/resources/etc.txt")
Put the resource folder inside target
getClass().getResource("/resources/etc.txt")
Put the resource folder inside target
answered Jan 1 at 16:18
J. AdamJ. Adam
1027
1027
add a comment |
add a comment |
There are two ways how you can get your resource:
- By absolute path, but itsn't good way, cause absolute paths are unreliable, but it is good for 'smoke'-testing
- By relative path, and there are two ways again.
In both ways you can use getClass().getResource(path) construction.
Relative paths:
- 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
- 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!
add a comment |
There are two ways how you can get your resource:
- By absolute path, but itsn't good way, cause absolute paths are unreliable, but it is good for 'smoke'-testing
- By relative path, and there are two ways again.
In both ways you can use getClass().getResource(path) construction.
Relative paths:
- 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
- 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!
add a comment |
There are two ways how you can get your resource:
- By absolute path, but itsn't good way, cause absolute paths are unreliable, but it is good for 'smoke'-testing
- By relative path, and there are two ways again.
In both ways you can use getClass().getResource(path) construction.
Relative paths:
- 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
- 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!
There are two ways how you can get your resource:
- By absolute path, but itsn't good way, cause absolute paths are unreliable, but it is good for 'smoke'-testing
- By relative path, and there are two ways again.
In both ways you can use getClass().getResource(path) construction.
Relative paths:
- 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
- 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!
edited Jan 1 at 20:52
answered Jan 1 at 17:08
aarexeraarexer
327211
327211
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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